use*_*002 10 java android onresume android-lifecycle android-activity
我正在重写我的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);
}
Run Code Online (Sandbox Code Playgroud)
下一步是收集用户输入,告诉我用户希望对报告进行哪些更改.(它可能是新位置,新日期或新显示样式等).这样做如下:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
如示例所示,在onResume()
确定新用户命令后通过调用重新生成输出.这是不好的做法,我已经知道了!! 然而,就我所确定的而言,它运作良好,老实说我不明白它的问题.
我的解决方案是将600行代码收集到一个单独的例程中,然后从内部onResume()
和内部的多个点调用它onOptionsItemSelected()
@Override
public void onResume() {
super.onResume();
myOnResumeCode();
}
Run Code Online (Sandbox Code Playgroud)
在里面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
}
Run Code Online (Sandbox Code Playgroud)
这种方法可以接受吗?如果没有,任何缺少"重写整个事情"的建议对我都非常有帮助.我已经广泛搜索了一个干净的解决方案,但找不到我能理解的解决方案.谢谢.
老实说,我不明白它的问题。
您的onResume()
方法实现本身是无害的。但是调用它的super方法super.onResume();
会让系统认为这是resume事件的另一次发生。这将导致刷新视图和类似的内部工作不必要的资源使用。因此,在任何情况下都必须避免显式调用生命周期回调方法。
这种方法可以接受吗?
代码行数并不能决定它是否可以接受。这是一个你需要问自己的问题。如果您认为整个代码将在该事件中执行,那么您应该这样做。否则你可以节省一些资源。
如果你正在做这样的事情
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();
}
Run Code Online (Sandbox Code Playgroud)
那么改成这样就值得了。
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();
}
Run Code Online (Sandbox Code Playgroud)
听完你的回答,我仔细看了你的问题,这让我大吃一惊。
我的计划是将这 600 行移动到一个单独的类文件中。当我在活动源文件中处理命令解码器时,这将使它们免受损坏
实际上不。但你们真的很亲近。忘记所有的复杂性,比如活动生命周期、方法、类等等,只关注计算机程序执行的最基本级别。
程序总是一行一行地执行。你如何安排代码没有任何区别。将程序正确构造为方法、类等是为了程序员的方便。对于系统来说,它总是一系列的线。因此,在执行繁重的任务时,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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是最基本的形式。当然还有其他选择(例如AsyncTask)。您可以轻松地在线找到更多相关信息(尝试搜索“Android 中的多线程”)。请随时询问更多信息。
归档时间: |
|
查看次数: |
1119 次 |
最近记录: |