#以下所有学习内容参考郭霖编写的《Android第一行代码》 按照书上的进度进行学习,该贴只是记录本人学习的笔记。
Android中的活动是可以层叠的。类似一个栈的结构。每当我们启动了一个新的活动,它会在返回栈中入栈,并处于栈顶的位置,当我们按下Back键或调用finish()方法销毁一个活动时,处于栈顶的活动会出栈,这时前一个入栈的活动就会重新处于栈顶的位置。系统总是会显示处于栈顶的活动给用户。
每个活动在其生命周期中最多可能会有四种状态: 1.运行状态:位于栈顶 2.暂停状态:不位于栈顶,但仍然可见 3.停止状态:不位于栈顶,完全不可见 4.销毁状态:从返回栈中移除
Activity类中定义了7个回调方法: onCreate():活动第一次创建时被调用,初始化操作 onStart():活动由不可见变为可见时调用 onResume():活动准备好与用户交互时调用,此时活动位于栈顶并处于运行状态 onPause():系统准备去启动或者恢复另一个活动时调用 onStop():活动完全不可见时调用 onDestroy():被销毁之前调用 onRestart():由停止状态变为运行状态之前调用
新建项目和活动: 修改normal_layout.xml和dialog_layout.xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is a normal activity."/> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is a dialog activity."/> </LinearLayout>这两个活动,一个是普通的活动,一个是对话框式的活动 注册:
<activity android:name=".NormalActivity" /> //android:theme属性:用来给当前活动指定主题,这里使用了对话框式主题 <activity android:name=".DialogActivity" android:theme="@style/Theme.AppCompat.Dialog"> </activity>修改activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/start_normal_activity" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start NormalActivity"/> <Button android:id="@+id/start_dialog_activity" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start DialogActivity"/> </LinearLayout>修改MainActivity中的代码:
package com.example.activitylifecycletest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.content.Intent; public class MainActivity extends AppCompatActivity { public static final String TAG="MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG,"onCreate"); setContentView(R.layout.activity_main); Button startNoramalActivity=(Button)findViewById(R.id.start_normal_activity); Button startDialogActivity=(Button)findViewById(R.id.start_dialog_activity); startNoramalActivity.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Intent intent=new Intent(MainActivity.this,NormalActivity.class); startActivity(intent); } }); startDialogActivity.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Intent intent=new Intent(MainActivity.this,DialogActivity.class); startActivity(intent); } }); } @Override protected void onStart(){ super.onStart(); Log.d(TAG,"onStart"); } @Override protected void onResume(){ super.onResume(); Log.d(TAG,"onResume"); } @Override protected void onPause(){ super.onPause(); Log.d(TAG,"onPause"); } @Override protected void onStop(){ super.onStop(); Log.d(TAG,"onStop"); } @Override protected void onDestroy(){ super.onDestroy(); Log.d(TAG,"onDestroy"); } @Override protected void onRestart(){ super.onRestart(); Log.d(TAG,"onRestart"); } }运行 ①
②点击第一个按钮,NormalActivity会把MainActivity全部遮住,因此onPause()和onStop()都会执行 ③按back ④点击第二个按钮,此时MainActivity依旧可见,只是onPause ⑤按back ⑥按back退出
注意:当一个活动进入到了停止状态,是有可能被系统回收的,因此被回收的活动要在被销毁前把数据保存下来,onSaveInstanceState()提供了这样一个功能。
//活动回收之前被调用,保存数据 @Override protected void onSaveInstanceState(Bundle outState){ super.onSaveInstanceState(outState); String tempData="something that you type"; outState.putString("data_key",tempData); }进行恢复:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG,"onCreate"); setContentView(R.layout.activity_main); //看是否有保存之前的数据 if(savedInstanceState!=null){ String tempData=savedInstanceState.getString("data_key"); Log.d(TAG,tempData); } ...拓展:Intent可以结合Bundle一起用于传递数据,首先可以把需要传递的数据都保存在Bundle对象中,然后再将Bundle对象存放在Intent里。到了目标活动再从Intent中取出Bundle,再从Bundle中一一取出数据。
活动的启动模式通过标签指定的android:launchMode属性来进行选择。
对于使用standard模式的活动,系统不会在乎这个活动是否已经在返回栈中存在,每次启动都会创建该活动的一个新的实例。每个实例都要按一次back才能退出。
当活动的启动模式指定为singleTop,在启动活动时如果发现返回栈的栈顶已经是该活动,则认为可以直接使用它,不会再创建新的活动实例。 但若活动不处于栈顶时,还是会创建新的实例。
可以让某个活动在整个应用程序的上下文中只存在一个实例。 每次启动该活动时系统首先会在返回栈中检查时候存在该活动的实例,如果发现已经存在则直接使用该实例,并把这个活动之上的所有活动都出栈,如果没有就会创建一个新的活动实例。
指定为singleInstance模式的活动会启用一个新的返回栈来管理这个活动,不管是哪个应用程序来访问这个活动,都共用的同一个返回栈。
