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

java– 直接onResume()调用的替代方法,第1张

概述我正在重写我的Android应用以消除对onResume()的直接调用.我的应用程序目前在onResume()内部完成大部分工作,然后发布显示,这是onResume()的结束.@OverridepublicvoidonResume(){super.onResume();//getcurrentdateandtime,//anddetermineifdaylig

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

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

@OverrIDepublic 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);}

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

@OverrIDepublic 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()中的多个点调用它.

@OverrIDepublic voID onResume() {    super.onResume();    myOnResumeCode();}

在onoptionsItemSelected()内部执行此 *** 作

@OverrIDepublic 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中的多线程”).随意问更多.

总结

以上是内存溢出为你收集整理的java – 直接onResume()调用的替代方法全部内容,希望文章能够帮你解决java – 直接onResume()调用的替代方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1104909.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存