activity直接销毁_Android 关于Activity的销毁和重建

Activity的销毁分为两种情况:

第一种是正常的销毁,比如用户按下Back按钮或者是activity自己调用了finish()方法;

另一种是由于activity处于stopped状态,并且它长期未被使用,或者前台的activity需要更多的资源,这些情况下系统就会关闭后台的进程,以恢复一些内存。

需要注意的是这其中有一种情况就是屏幕旋转的问题,当用户旋转手机屏幕,每一次都会导致activity的销毁和重新建立。

对于重建情况,尽管实际的activity实例已经被销毁,但是系统仍然记得它的存在,当用户返回到它的时候,系统会创建出一个新的实例来代替它,这里需要利用旧实例被销毁时候存下来的数据。这些数据被称为“instance state”,是一个存在Bundle对象中的键值对集合。

缺省状态下,系统会把每一个View对象保存起来(比如EditText对象中的文本,ListView中的滚动条位置等),即如果activity实例被销毁和重建,那么不需要你编码,layout状态会在onRestoreInstanceState后恢复到前次状态,可见Activity的onRestoreInstanceState。

protected void onRestoreInstanceState(Bundle savedInstanceState) {

if (mWindow != null) {

Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);

if (windowState != null) {

mWindow.restoreHierarchyState(windowState);

}

}

}

如果重写了onRestoreInstanceState并且为super Activity的onRestoreInstanceState,界面将不会恢复。

但是如果你的activity需要恢复更多的信息,比如成员变量信息,则需要自己动手写了。

如果要存储额外的数据,必须覆写回调函数onSaveInstanceState(),注意super.onSaveInstanceState()。系统会在用户离开activity的时候调用这个函数,并且传递给它一个Bundle object,如果系统稍后需要重建这个activity实例,它会传递同一个Bundle object到onRestoreInstanceState() 和 onCreate() 方法中去。

1.jpg

当系统停止activity时,系统会调用onSaveInstanceState(),状态信息会以键值对的形式存储下来,程序可以在onSaveInstanceState保存自己想要保存的变量。

static final String STATE_SCORE = "playerScore";

static final String STATE_LEVEL = "playerLevel";

...

@Override

public 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);

}

要记得调用基类的实现,以实现默认的实现

在onCreate中,可以先检查是否Bundle是null,如果是null,则表明是要创建一个全新的对象,而不是重建一个上次被销毁的对象。

@Override

protected 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

}

...

}

也可以选择在onRestoreInstanceState()中实现,这个函数在onStart()之后调用。只有在有数据要恢复的时候系统会调用onRestoreInstanceState()

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);

}

此处也要注意,不要忘记调用基类实现。

引用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值