gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:30:01 26 4
gpt4 key购买 nike

我正在重写我的 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 方法 super.onResume(); 会让系统认为这是 resume 事件的另一次发生。这将导致不必要的资源使用用于刷新 View 和类似的内部工作。因此,在任何情况下都必须避免显式调用生命周期回调方法。

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

实际上不是。但你真的很亲密。忘记所有复杂性,例如 Activity 生命周期、方法、类等等,只关注计算机程序的最基本执行级别。

程序总是逐行执行的。您如何安排代码没有任何区别。将程序适本地组织成方法、类等是为了程序员的方便。对于系统来说,它总是一系列的线。因此,在执行繁重的任务时,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 中的多线程”)。欢迎询问更多。

关于java - 直接调用 onResume() 的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50643571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com