- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在重写我的 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/
为了让我的代码几乎完全用 Jquery 编写,我想用 Jquery 重写 AJAX 调用。 这是从网页到 Tomcat servlet 的调用。 我目前情况的类似代码: var http = new
我想使用 JNI 从 Java 调用 C 函数。在 C 函数中,我想创建一个 JVM 并调用一些 Java 对象。当我尝试创建 JVM 时,JNI_CreateJavaVM 返回 -1。 所以,我想知
环顾四周,我发现从 HTML 调用 Javascript 函数的最佳方法是将函数本身放在 HTML 中,而不是外部 Javascript 文件。所以我一直在网上四处寻找,找到了一些简短的教程,我可以根
我有这个组件: import {Component} from 'angular2/core'; import {UserServices} from '../services/UserService
我正在尝试用 C 实现一个简单的 OpenSSL 客户端/服务器模型,并且对 BIO_* 调用的使用感到好奇,与原始 SSL_* 调用相比,它允许一些不错的功能。 我对此比较陌生,所以我可能会完全错误
我正在处理有关异步调用的难题: 一个 JQuery 函数在用户点击时执行,然后调用一个 php 文件来检查用户输入是否与数据库中已有的信息重叠。如果是这样,则应提示用户确认是否要继续或取消,如果他单击
我有以下类(class)。 public Task { public static Task getInstance(String taskName) { return new
嘿,我正在构建一个小游戏,我正在通过制作一个数字 vector 来创建关卡,该数字 vector 通过枚举与 1-4 种颜色相关联。问题是循环(在 Simon::loadChallenge 中)我将颜
我有一个java spring boot api(数据接收器),客户端调用它来保存一些数据。一旦我完成了数据的持久化,我想进行另一个 api 调用(应该处理持久化的数据 - 数据聚合器),它应该自行异
首先,这涉及桌面应用程序而不是 ASP .Net 应用程序。 我已经为我的项目添加了一个 Web 引用,并构建了各种数据对象,例如 PayerInfo、Address 和 CreditCard。但问题
我如何告诉 FAKE 编译 .fs文件使用 fsc ? 解释如何传递参数的奖励积分,如 -a和 -target:dll . 编辑:我应该澄清一下,我正在尝试在没有 MSBuild/xbuild/.sl
我使用下划线模板配置了一个简单的主干模型和 View 。两个单独的 API 使用完全相同的配置。 API 1 按预期工作。 要重现该问题,请注释掉 API 1 的 URL,并取消注释 API 2 的
我不确定什么是更好的做法或更现实的做法。我希望从头开始创建目录系统,但不确定最佳方法是什么。 我想我在需要显示信息时使用对象,例如 info.php?id=100。有这样的代码用于显示 Game.cl
from datetime import timedelta class A: def __abs__(self): return -self class B1(A):
我在操作此生命游戏示例代码中的数组时遇到问题。 情况: “生命游戏”是约翰·康威发明的一种细胞自动化技术。它由一个细胞网格组成,这些细胞可以根据数学规则生存/死亡/繁殖。该网格中的活细胞和死细胞通过
如果我像这样调用 read() 来读取文件: unsigned char buf[512]; memset(buf, 0, sizeof(unsigned char) * 512); int fd;
我用 C 编写了一个简单的服务器,并希望调用它的功能与调用其他 C 守护程序的功能相同(例如使用 ./ftpd start 调用它并使用 ./ftpd stop 关闭该实例)。显然我遇到的问题是我不知
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
我希望能够从 cmd 在我的 Windows 10 计算机上调用 python3。 我已重新安装 Python3.7 以确保选择“添加到路径”选项,但仍无法调用 python3 并使 CMD 启动 P
我是一名优秀的程序员,十分优秀!