重新启动 Android 中的 Activity
我有一项活动 A,它有一个按钮和一个显示书籍名称的列表视图。单击按钮后,活动 B 开始,用户填写图书表格并保存。当他按下后退按钮时,用户来到活动 A。这里应该在列表视图中更新书名。我想我必须在 onResume() 中编写一些代码。你能告诉我要写什么吗?我正在使用自定义列表视图。
I have one activity A, that has one button and one list view which shows names of books . on click of the button, activity B starts, there user fill the book form and save it . when he press back button , user comes to activity A. Here the book name should be updated in listview. I think I have to write some code in onResume() . Can u please tell me what to write. I am using customised list view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
startActivityForResult()
启动 Activity B 并使用方法onActivityResult()
重新启动或处理新数据例如,启动 Activity B:
然后在 Activity A 类中的某个位置:
其他答案应该足够了,但是在通过其他方式恢复活动的情况下可以调用 onResume() 。
要在用户从 Activity B 按下后退按钮时简单地重新启动 Activity A,然后将以下内容放入 onActivityResult 中:
并在 Activity A 的 onCreate 中添加
starterintent = getIntent();
只需记住初始化变量在调用 onCreate 之前使用
Intent starterintent;
。例如
然后只需通过单击按钮或其他方式调用
startActivityB()
Start activity B with
startActivityForResult()
and use methodonActivityResult()
to restart or process the new dataFor example, to start Activity B:
Then somewhere in your Activity A class:
The other answers should suffice, but onResume() can be called in cases where the activity is resumed by other means.
To simply restart Activity A when user presses back button from Activity B, then put the following inside the onActivityResult:
And in the onCreate of Activity A, add
starterintent = getIntent();
Just remember to initiate the variable with
Intent starterintent;
somewhere before your onCreate is called.e.g.
Then just call
startActivityB()
from a button click or whatever是的你是对的。在onResume中编写代码。
当您更新日期时,只需调用 notifyDataSetChanged();为您的 ListView 适配器
希望,它对您有帮助!
YES you are right. Write code in onResume.
When you updated date just call notifyDataSetChanged(); for your ListView adapter
Hope, it help you!
您可以在用户按“保存”时启动活动,它会为您修复它。
或者如果您想按回:
You can either start the activity when user press on Save, and it will fix it for you.
Or if you want to press back: