android-Recreating an Activity
来源:互联网 发布:严莉莉变身器淘宝 编辑:程序博客网 时间:2024/05/24 01:46
when the user presses the Back button or your activity signals its own destruction by calling finish()
. The system may also destroy your activity if it's currently stopped and hasn't been used in a long time or the foreground activity requires more resources so the system must shut down background processes to recover memory.
When your activity is destroyed because the user presses Backor the activity finishes itself, the system's concept of thatActivity
instance is gone forever because the behavior indicates the activity is no longer needed.
By default, the system uses the Bundle
instance state to save information about each View
object in your activity layout (such as the text value entered into an EditText
object). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you.
Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id
attribute.
> To save additional data about the activity state, you must override the onSaveInstanceState()
callback method. The system calls this method when the user is leaving your activity and passes it the Bundle
object that will be saved in the event that your activity is destroyed unexpectedly. If the system must recreate the activity instance later, it passes the same Bundle
object to both the onRestoreInstanceState()
andonCreate()
methods.
static final String STATE_SCORE = "playerScore";static final String STATE_LEVEL = "playerLevel";...@Overridepublic void onSaveInstanceState(Bundle savedInstanceState) { // Save the user's current game state savedInstanceState.putInt(STATE_SCORE, mCurrentScore); savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState);}
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Always call the superclass first // Check whether we're recreating a previously destroyed instance if (savedInstanceState != null) { // Restore value of members from saved state mCurrentScore = savedInstanceState.getInt(STATE_SCORE); mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); } else { // Probably initialize members with default values for a new instance } ...}
public void onRestoreInstanceState(Bundle savedInstanceState) { // Always call the superclass so it can restore the view hierarchy super.onRestoreInstanceState(savedInstanceState); // Restore state members from saved instance mCurrentScore = savedInstanceState.getInt(STATE_SCORE); mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);}Caution: Always call the superclass implementation of
onRestoreInstanceState()
so the default implementation can restore the state of the view hierarchy. - android-Recreating an Activity
- Recreating an Activity
- Recreating an Activity
- 08Recreating an Activity
- Recreating an Activity
- Recreating an Activity 重新创建一个活动
- Android官网恢复(重新创建)Activity文档翻译:Recreating an Activity
- Android官方Training阅读笔记 ---- Managing the Activity Lifecycle(Recreating an Activity) (四)
- Android---Activity 生命周期(四)Recreating Activity
- Android学习路线(十五)Activity生命周期——重新创建(Recreating)一个Activity
- Android学习路线(十五)Activity生命周期——重新创建(Recreating)一个Activity
- Activity生命周期之重新创建Activity(recreating activity)
- Reloading an activity in Android
- Android Activity生命周期 之starting an activity
- Android: Capturing the return of an activity
- android Training Stopping and Restarting an Activity
- Android training 管理Activity生命周期之启动Activity(Starting an Activity)
- Android training 管理Activity生命周期之暂停/恢复Activity(Pausing and Resuming an Activity)
- iOS笔记UI--修改系统UITableViewCell的ImageView大小
- java 开源资源
- 【分治法】循环赛事表
- 【poj1741】Tree 点分治
- codevs网站 天梯 青铜级
- android-Recreating an Activity
- NDK开发,没有你想象的那么难
- 初学 数据库
- Java垃圾回收机制算法
- Java垃圾回收机制
- 课程设计
- codevs 天梯 白银级
- 数据库理论第一章
- 图片3D翻转效果