我正在重写Android APP,以消除对onResume ()的直接调用。

我的APP应用程序目前在onResume ()内部完成了大部分工作,并公开了显示。 这是onResume ) )的结尾。

@Override

公共语音oid on resume

super.onResume (;

//获取当前日期和时间,

//anddetermineifdaylightsavingstimeisineffect。

//.600 lines of code

//output both the chart and report

//

image.setimagebitmap (height chart );

报告. setimagebitmap (报告位图;

}

下一步是收集用户输入并进行哪些更改?

报告用户的意愿。 (它可能是新的位置、新的日期或新的视觉样式等。 )就这样做。

@Override

publicbooleanonoptionsitemselected (维护项目) {

final int STATION_REQUEST=1;

int test=1;

sitch(item.getitemid ) ) }

case R.id.refresh: {

用户日期集=false;

onResume (;

返回真;

} //come to the present。

//.200 lines of code

默认:

return super.onoptionsitemselected (item;

}

}

如例所示,确定新的用户命令后,调用onResume ) )重新生成输出.我已经知道这是不好的做法! 但是,只要我确定,它就会运作良好,老实说我不理解那个问题。

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

@Override

公共语音oid on resume

super.onResume (;

myOnResumeCode (;

}

此操作在onOptionsItemSelected ()中执行

@Override

publicbooleanonoptionsitemselected (维护项目) {

final int STATION_REQUEST=1;

int test=1;

sitch(item.getitemid ) ) }

case R.id.refresh: {

用户日期集=false;

myOnResumeCode (;

返回真;

} //come to the present。

.//Other statements

}

这个方法可以接受吗? 否则,缺乏“重写整个事情”的建议对我很有帮助.我已经广泛搜索了干净的解决方案,但没有找到我能理解的解决方案.谢谢.

解决方法:

ihonestlydonotunderstandtheproblemwithit。

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

Is this method acceptable?

不能接受代码行数。 这是你应该问自己的问题。 如果你认为整个代码在那个事件中运行,你就应该这样做。 否则,可以节约资源。

如果你在做这样的事

publicbooleanonoptionsitemselected (维护项目) {

sitch(item.getitemid ) ) }

caser.id.mnuenablesomething :

{

refreshTheWholeUi (;

返回真;

}

case R.id.mnuClearList:

{

refreshTheWholeUi (;

返回真;

/p>

}

}

}

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