Android 返回时刷新 Activity
我需要一点帮助来刷新我的应用程序中的一项活动。我正在使用选项卡主机活动并连接到 Web 服务并从我的子活动之一下载一些数据。当我在子活动中按下同步按钮时,我将启动一个不在选项卡主机中的新活动,同步完成后,它会返回到其父活动(子活动)。我想要实现的目标是当我返回活动时刷新活动。当我在互联网上检查时,我发现最好的选择是使用 startActivityForResult
,但我真的不明白如何使用它以及如何在收到结果时刷新活动完成活动。
如果有人能帮助我,我会很高兴。谢谢!
编辑:
Synchronization.class中显示日志
我正在使用此代码,它甚至没有在 onActivityResult
MyCollectionId.class :
Intent intent = new Intent(MyCollectionId.this, Synchronization.class);
intent.putExtra("process", 2);
startActivityForResult(intent, 1);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
Log.e("","OnActivityResult");
Intent refresh = new Intent(this, MyCollectionId.class);
startActivity(refresh);
this.finish();
}
}
:
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
setResult(RESULT_OK,intent);
finish();
I need a little help with refreshing one of my activities in my application. I'm using tab host activity and connecting to web service and downloading some data from one of my child activities. When I press sync button in my child activity I'm starting a new activity which is not in tab host and when the sync is done, it returns to it's parent (child activity). The thing that I want to achieve is to refresh the activity when I return back to it. As I checked over the internet I find that the best option to do it is using startActivityForResult
,but I don't really understand how to use that and how to refresh the activity when I receive the result from the finished activity.
If anyone can help me, I'll be really glad.Thanks!
EDIT:
I'm using this code and it's not even showing the Log in onActivityResult
MyCollectionId.class :
Intent intent = new Intent(MyCollectionId.this, Synchronization.class);
intent.putExtra("process", 2);
startActivityForResult(intent, 1);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
Log.e("","OnActivityResult");
Intent refresh = new Intent(this, MyCollectionId.class);
startActivity(refresh);
this.finish();
}
}
Synchronization.class :
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
setResult(RESULT_OK,intent);
finish();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
另一种棘手的方法是在
onRestart()
上启动您的活动,这实际上应该可以解决问题。 (在这段代码中,我展示了当您使用自定义 TabActivity 管理器时它的完成方式。)
Another tricky way to do this is just start your activity on
onRestart()
That should do the trick actually. (In this code I'm showing the way it's done when you are using a custom TabActivity manager.)
您应该在方法中处理在父活动中以“startActivityForResult”启动的活动的结果:
并且根据结果,您可以再次调用负责显示父活动中的信息的代码(可能是您将其放在进入 onResume() 方法或类似的方法)。
我建议您将负责信息渲染的所有逻辑移至单独的方法中。并在收到结果后调用它。而不是重新启动您的父活动。
You should handle the result of activity that you started with "startActivityForResult" in a parent activity in a method:
And depending on result you could just invoke again a code that is responsible for showing the information in your parent activity (may be you put it into onResume() method or like that).
I would suggest you to move all the logic responsible for information rendering to a separate method. And invoke it after you recieve the result. Instead of restarting your parent activity.
如果您使用
startActivityForResult
方法启动第二个 Activity,则当您返回第一个 Activity 时,将调用第一个 Activity 的onActivityResult
。如果您覆盖它,您可以从那里刷新您的活动。
查看更多信息 这里和此处
If you start your second activity with the method
startActivityForResult
, when you return to the first activity,onActivityResult
of the first activity will be called.If you override it, you can refresh your activity from there.
See more information here and here
使用带有请求代码的 startActivityForResult 调用子活动,子活动中的 SetResult。当子活动完成后,您可以在 onActivityResult 方法中更新父活动
这是一个示例 http://rahulonblog.blogspot.com/2010/05/android-startactivityforresult-example.html
Call child activity using startActivityForResult with request code,SetResult from the child Activity. And when the child activity is finished, you can update you parent activity in onActivityResult method
Here is an example http://rahulonblog.blogspot.com/2010/05/android-startactivityforresult-example.html
重写您想要刷新的 Activity 内的
onRestart()
方法,并重新创建该 ActivityOverride
onRestart()
method inside activity you want to refresh, and recreate the activity覆盖
方法,以便当您从所需任务返回时自动调用它。
将以下代码放入 onRestart() 方法中:
Override the
method so that it is called automatically when you return back from your desired task.
Put the following code in your onRestart() method :
按下按钮:
然后在同一个活动类中:
同步活动将具有:
on button press:
Then in the same Activity class:
The Sync Activity would have: