一、体验活动的生命周期的执行
代码组成:
1、三个Java类:MainActivity.java、NormalActivity.java、DialogActivity.java
2、三个布局文件:activity_main.xml、activity_normal.xml、activity_dialog.xml
3、程序入口:AndroidManifest.xml
4、创建一个过滤器:MainActivity
下面是具体代码:
MainActivity.java
1 package com.example.administrator.activitylifecycletest; 2 3 import android.content.Intent; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.util.Log; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.Toast; 10 11 public class MainActivity extends AppCompatActivity { 12 public static final String TAG="MainActivity"; 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 Log.d(TAG,"onCreate"); 18 Button startNormalActivity= findViewById(R.id.start_normal_activity); 19 Button startDialogActivity= findViewById(R.id.start_dialog_activity); 20 21 startDialogActivity.setOnClickListener(new View.OnClickListener() { 22 @Override 23 public void onClick(View view) { 24 Intent intent=new Intent(MainActivity.this,DialogActivity.class); 25 startActivity(intent); 26 } 27 }); 28 29 startNormalActivity.setOnClickListener(new View.OnClickListener() { 30 @Override 31 public void onClick(View view) { 32 Intent intent=new Intent(MainActivity.this,NormalActivity.class); 33 startActivity(intent); 34 } 35 }); 36 } 37 38 @Override 39 protected void onStart() { 40 super.onStart(); 41 Log.d(TAG,"onStart"); 42 } 43 44 @Override 45 protected void onResume() { 46 super.onResume(); 47 Log.d(TAG,"onResume"); 48 } 49 50 @Override 51 protected void onPause() { 52 super.onPause(); 53 Log.d(TAG,"onPause"); 54 } 55 56 @Override 57 protected void onStop() { 58 super.onStop(); 59 Log.d(TAG,"onStop"); 60 } 61 62 @Override 63 protected void onRestart() { 64 super.onRestart(); 65 Log.d(TAG,"onRestart"); 66 } 67 68 @Override 69 protected void onDestroy() { 70 super.onDestroy(); 71 Log.d(TAG,"onDestroy"); 72 } 73 }
NormalActivity.java
1 package com.example.administrator.activitylifecycletest; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 6 public class NormalActivity extends AppCompatActivity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_normal); 12 } 13 }
DialogActivity.java
1 package com.example.administrator.activitylifecycletest; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 6 public class DialogActivity extends AppCompatActivity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_dialog); 12 } 13 }
activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <Button 8 android:id="@+id/start_normal_activity" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:text="Start NormalActivity"/> 12 13 <Button 14 android:id="@+id/start_dialog_activity" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:text="Start DialogActivity"/> 18 19 </LinearLayout>
activity_normal.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <TextView 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:text="This is a normal activity"/> 11 </LinearLayout>
activity_dialog.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <TextView 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:text="This is a dialog activity"/> 11 </LinearLayout>
AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.administrator.activitylifecycletest"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:roundIcon="@mipmap/ic_launcher_round" 10 android:supportsRtl="true" 11 android:theme="@style/AppTheme"> 12 <activity android:name=".MainActivity"> 13 <intent-filter> 14 <action android:name="android.intent.action.MAIN" /> 15 <category android:name="android.intent.category.LAUNCHER" /> 16 </intent-filter> 17 </activity> 18 <activity android:name=".NormalActivity" /> 19 <activity android:name=".DialogActivity" 20 android:theme="@style/Theme.AppCompat.Dialog"> 21 </activity> 22 <!--android:theme是设置活动的主题,上面是将活动设置成了对话框式的活动--> 23 </application> 24 25 </manifest>
MainActivity过滤器:
代码运行结果:
1、运行程序,打印出一下日志:创建项目的时候运行了三个方法:onCreate()、onStart()、onResume()
2、点击Start NormalActivity:此时调用了两个方法:onPause()、onStop()
3、点击Back键:此时调用了三个方法:onRestart()、onStart()、onResume()
4、点击Start DialogActivity:DialogActivity是对话框式的活动,因此启动该活动时,只会调用onPause()方法,不会调用onStop()方法
5、点击Back键:相应的对话框式的活动按Back键后也是只调用onResume()方法。
6、再次按Back退出程序后:调用了三个方法:onPause()、onStop()、onDestroy()
二、活动被回收后临时数据的保存(onSaveInstanceState方法)
现象描述:MainActivity活动中有一个文本输入框,现在在文本框中输入了一段文字后,没有关闭该文本输入框,然后打开活动NormalActivity,这个时候MainActivity由于系统内存不足被回收了,过了一会儿又点击Back键回到MainACtivity,发现刚刚输入的文本信息全部清空了,这是因为MainActivity被重建了。
实际需求:在NormalActivity活动回到MainActivity活动后,我们需要之前的数据保留下来。
问题解决:Activity类提供了一个onSaveInstanceState()方法,这个方法保证活动在被回收前一定调用,我们可以通过这个方法来解决活动被回收时临时数据得不到保存的问题。
保存临时数据:
在MainActivity.java(有临时数据的活动)中添加如下代码:
获取临时数据:
修改MainActivity.java(有临时数据的活动)中onCreate()方法中的代码:下面通过Log来打印出那些临时数据的。
拓展:
使用Bundle来保存和取出数据,与Intent传递数据基本类似。Intent可以结合Bundle一起用于传递数据:
思路:先把数据保存在Bundle对象中,在把Bundle对象放在Intent里。到了目标活动后,先从Intent中取出Bundle,再从Bundle中一一取出数据。