您的位置:首页 > 移动开发 > Android开发

Android学习 -- 《关于Activity》

2016-05-11 21:40 435 查看

activity的生命周期由以下

名称注释
Created暂态,一瞬间,用于创造
started暂态,一瞬间,可见
resumed可交互,最顶层
paused半透明,被遮挡,不执行代码
stoped不可见,状态被保留
调用关系如下:



指定launcher

一个app里有多个activity的,哪一个第一个加载呢?

<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


只有制定了 MAIN 和 LAUNCHER 的activity 才会在桌面显示图标,如果有多个activity具有两者,那么这个app在桌面会有多个图标,对应多个activity

创建实例

oncreat() 方法用来创建一个新的activity

那么它做了哪些事情呢?

声明UI元素,定义成员变量等
mTextView = (TextView) findViewById(R.id.text_message); //声明了一个textview


setcontentView
也很重要。之后会提到

只有在oncreate()运行完之后进入started 后用户才会看到 界面,所以oncreate()里面不要写太多的东西。

Activity的暂停和恢复

从 resumed 到 paused 状态,调用的是 OnPause()

触发:当前activity部分可见,被其他组件阻塞

作用:

1. 停止动画和正在进行的操作,节约cpu
2. 提交用户离开期间的保存内容,如 邮件
3.  释放一部分系统资源,比如 GPS,broadcast receiver等


示例:

@Override
public void onPause() {
super.onPause();  // Always call the superclass method first

// Release the Camera because we don't need it when paused
// and other activities might need to use it.
if (mCamera != null) {
mCamera.release() //释放了camera,节约了内存
mCamera = null;
}
}


注意:

不应该适用onpause()去永久保存数据,除非用户期待。

写数据到DB会让切换很慢

heavy-load应该交给onstop()做

在恢复的时候,再次初始化哪些被释放掉的组件,比如刚才的camera

@Override
public void onPause() {
super.onPause();  // Always call the superclass method first
// Release the Camera because we don't need it when paused
// and other activities might need to use it.
if (mCamera != null) {
mCamera.release()
mCamera = null;
}
}


停止和重启

onStop()



onRestart()

在调用 onstop 之前一定是调用了 onpause的

如果activity停止,系统会在 需要内存空间时 摧毁它的实例和栈结构。

所以一定要用onstop释放内存,避免泄露

OnStop() 可以执行要大量使用cpu的操作,比如写入数据库。

每次可见都是调用了 OnStart 的

而OnRestart则是在从stop恢复才用

重新创建Activity

在长时间不使用,或者前台activity需要更多资源的情况下关闭进程。

如果发生了这种情况,系统会在 Bundle对象中存在一些 键值对 key-value pairs 来保存destroy时刻 activity的状态

之后再用这些数据来重新创建一个实例

这些存储状态的数据就是 instance state

屏幕旋转 会导致 activity 被destroy 并 recreatd

注意:

每一个view都对应着唯一的一个android:id,为了恢复之前的view状态

重要的方法:

OnSaveInstanceState() 这个方法在用户离开activity的时候调用,调用时,系统会在activity被异常销毁的时 传递 Bundle 对象,我们可以增加 信息到 Bundle 对象中

之后再想重新创建,则之前的 Bundle对象 被传递到 OnCreate() 与 OnRestoreInstanceState() 中

示例代码:

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// 保存了玩家的分数和级别,以键值对的形式存入
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);
}


重点如何恢复 :

示例代码:

@Override
protected void onCreate(Bundle savedInstanceState) { //终于知道这个Bundle传入的是什么了 =V=

super.onCreate(savedInstanceState); //

// 检查是否为空,如果为空则初始化,如果不为空,则从其中取出相关数据
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
}
...
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: