返回上一个活动时结果一致
我有一个 Android 应用程序,有各种活动。
例如,有一个主屏幕和一个更新屏幕。
主屏幕是一个列表活动。
主屏幕上的按钮会将您带到更新屏幕,该屏幕可从服务器更新数据库。理论上,当更新后返回主屏幕时,应该更改列表以反映刚刚完成的更新。
转到更新屏幕的代码如下:
Button synch = (Button) findViewById(R.id.synchButton);
synch.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
Intent intent = new Intent(HomeScreen.this, SynchScreen.class);
startActivity(intent);
}
});
更新后返回主屏幕的代码为:
main_menu.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
finish();
}
});
该列表是从 onStart 中运行的异步任务编译的,所以我的理解是 onStart 应该在我返回时运行到主屏幕,从而始终显示列表的最新版本。
在我的模拟器上,我总是会得到更新的列表,但是当我在手机上运行它时,列表永远不会更新,它只是返回到更新之前的屏幕状态。
有什么想法吗?
谢谢
凯文
I have an android app that has various activities.
for example there is a home screen and an update screen.
The home screen is a list activity.
A button on the home screen brings you to the update screen that updates the database from a server. In theory when Returning to teh homescreen after an update, the list should be changed to reflect the update just done.
the code for going to the update screen is as follows:
Button synch = (Button) findViewById(R.id.synchButton);
synch.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
Intent intent = new Intent(HomeScreen.this, SynchScreen.class);
startActivity(intent);
}
});
and the code for returning back to the homescreen after the updates is:
main_menu.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
finish();
}
});
the list is compiled from an async task that runs in onStart, so my understanding is that onStart should run when I return to the homescreen, thus always displaying the most up to date version of the list.
On my Emulator I always get teh updated list, but when I run it on my phone the list is never updated, it just returns to the state of teh screen before I did the update.
any ideas?
thanks
Kevin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请查看 Android 文档的活动生命周期部分。更新视图的代码可能应该移至 onResume,因为启动新活动时该 Activity 可能不会被终止。
Check the Activity lifecycle section of the Android documentation. The code updating the view should probably be moved to onResume, since the Activity might not get killed when launching a new one.
将启动
Asynctask
的代码放在onResume
中。阅读与活动生命周期相关的文档。Put the code for starting the
Asynctask
inonResume
. Read the documentation related to activity life cycle.