编程语言
首页 > 编程语言> > java – 直接onResume()调用的替代方法

java – 直接onResume()调用的替代方法

作者:互联网

我正在重写我的Android应用以消除对onResume()的直接调用.

我的应用程序目前在onResume()内部完成大部分工作,然后发布显示,这是onResume()的结束.

@Override
public void onResume() {
    super.onResume();
    // get current date and time,
    //  and determine if daylight savings time is in effect.
    //...600 lines of code
    // output both the chart and report
    //
    image.setImageBitmap(heightChart);
    report.setImageBitmap(reportBitmap);
}

下一步是收集用户输入,告诉我对其进行了哪些更改
报告用户的意愿. (它可能是新位置,新日期或新显示样式等).这样做如下:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    final int STATION_REQUEST = 1;
    int test = 1;
    switch (item.getItemId()) {
        case R.id.refresh: {
            userDateSet = false;
            onResume();
            return true;
        } // come to the present.

        //...200 lines of code
        default:
            return super.onOptionsItemSelected(item);
    }
}

如示例所示,在确定新用户命令后,通过调用onResume()重新生成输出.这是不好的做法,我已经知道了!!然而,就我所确定的而言,它运作良好,老实说我不明白它的问题.

我的解决方案是将600行代码收集到一个单独的例程中,并从onResume()内部和onOptionsItemSelected()中的多个点调用它.

@Override
public void onResume() {
    super.onResume();
    myOnResumeCode();
}

在onOptionsItemSelected()内部执行此操作

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    final int STATION_REQUEST = 1;
    int test = 1;
    switch (item.getItemId()) {
        case R.id.refresh: {
            userDateSet = false;
            myOnResumeCode();
            return true;
        } // come to the present.

    ... // Other statements
}

这种方法可以接受吗?如果没有,任何缺少“重写整个事情”的建议对我都非常有帮助.我已经广泛搜索了一个干净的解决方案,但找不到我能理解的解决方案.谢谢.

解决方法:

I honestly do not understand the problem with it.

你的onResume()方法实现本身是无害的.但是调用它的超级方法是super.onResume();会让系统认为它是恢复事件的另一种情况.这将导致刷新视图和类似内部工作的不必要的资源使用.因此,在任何情况下都必须避免显式调用生命周期回调方法.

Is this method acceptable?

代码行数不会使其可接受.这是一个你需要问自己的问题.如果您认为整个代码将在该事件中执行,那么您应该这样做.否则你可以节省一些资源.

如果你正在做这样的事情

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.mnuEnableSomething:
            {
                refreshTheWholeUi();
                return true;
            }
        case R.id.mnuClearList:
            {
                refreshTheWholeUi();
                return true;
            }
    }
}

public void onResume() {
    super.onResume();
    refreshTheWholeUi();
}

然后将其更改为此值得.

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.mnuEnableSomething:
            {
                enableIt();
                return true;
            }
        case R.id.mnuClearList:
            {
                justClearTheList();
                return true;
            }
    }
}

public void onResume() {
    super.onResume();
    refreshTheWholeUi();
}

现在,以该主题为核心

回答后,我仔细看了一下你的问题,这让我大吃一惊.

My plan is to move those 600 lines to a separate class file. That will
keep them away from damage while I work on the command decoder in the
activity source file

并不是.但你真的很亲密.忘掉活动生命周期,方法,类等所有复杂性,只关注计算机程序的最基本执行级别.

程序总是逐行执行.如何安排代码没有任何区别.将程序正确地构造成方法,类等是为了程序员的方便.对于系统来说,它始终是一系列的线条.因此,在执行繁重的任务时,UI可能变得没有响应,因为它必须等到轮到它.

那么如何并行工作呢?

多线程…!

它听起来并不那么复杂.

您必须找到代码中最关键的部分,它更多地使用资源并将其移动到不同的线程.

我已经说明了如何在这里进行多线程.

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.mnuProceessImageAction:
            {
                //Let user know that a background operation is running
                //with a progressbar or something
                processImage(mImage);
                return true;
            }
    }
}

private void processImage(Object image) {
    new Thread(new Runnable(){
        public void run() {
        //Doing all the heavy duty here.
        //.............................
        //Now you have the result. Use it to update the UI.
        //(UI can be updated only from the UI thread)
        runOnUiThread(new Runnable(){
                public void run() {
                    updateTheUiWithTheImage(proccessedImage);
                }
        });
        }
    }).start();

}

private void updateTheUiWithTheImage(Object image) {
    try {
        //Hide progressbar if you have one
        //Now it wont make the UI to struggle to use it.
    } catch(NullPointerException e) {
        e.printStackTrace;
    }
}

这是最基本的形式.当然还有其他选择(如AsyncTask).您可以在线轻松找到更多相关信息(尝试搜索“Android中的多线程”).随意问更多.

标签:onresume,android,java,android-activity,android-lifecycle
来源: https://codeday.me/bug/20190910/1798329.html