前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java中的onresume_java – 直接onResume()调用的替代方法

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

作者头像
全栈程序员站长
发布2022-09-05 11:28:18
9060
发布2022-09-05 11:28:18
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

我正在重写我的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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/137231.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年6月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档