Android - Activity 和 startActivity
我有 2 个活动 Activity1 和 Activity2。
我从 Activity1 导航到 Activity2(调用 startActivity),当我单击返回时,它会启动一个全新的 Activity1。
有没有办法让 Activity1 保持活动状态以保持其状态?
I have 2 activities Activity1 and Activity2.
I navigate from Activity1 to Activity2 (Call startActivity), and when I click back it starts a brand new Activity1.
Is there a way to keep Activity1 alive so that it keeps its state?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您从 Activity 执行 startActivity() 时,Android 会暂停当前 Activity 并将其置于后台。此时,将调用当前活动的 onPause() 方法。
另外,如果想保存当前状态,也可以重写onSavedInstanceState()方法。
查看其中一些参考资料以及 Android 生命周期。
http://developer.android.com/reference/android/app/Activity.html
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
When you do a startActivity() from an activity, Android pauses the current activity and puts it in the background. For that moment, onPause() method of current activity is called.
In addition, if you want to save the current state, you can also override the onSavedInstanceState() method.
Check out some of these references as well as the Android Life Cycle.
http://developer.android.com/reference/android/app/Activity.html
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
当然有办法。
每次启动新活动时,前一个活动都会停止,但系统会将该活动保留在堆栈中(“后堆栈”)。
http://developer.android.com/guide/topics /fundamentals/tasks-and-back-stack.html
Ofcourse there is a way.
Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack").
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html