體驗活動的生命周期
阿新 • • 發佈:2018-11-15
allow backup des bundle key .com ima test tro
體驗活動的生命周期
Android活動的生命周期圖如下:
項目步驟:
1.創建一個新項目ActicityLifeCycleTest
2.創建活動DialogActivity和NormalActivity
3.修改activity_normal.xml和activity_dialog.xml,如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.acticitylifecycletest.NormalActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="this is a normalActivity" /> </android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.acticitylifecycletest.DialogActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="this is dialogActivity" /> </android.support.constraint.ConstraintLayout>
修改AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.acticitylifecycletest"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".NormalActivity" android:label="這個是正常的窗口" > </activity> <!-- 主要修改DialogActivity的相關內容 android:theme="@android:style/Theme.Dialog" 是讓DialogActivity使用對話框樣式 --> <activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dialog" android:label="這個是對話框的窗口" > </activity> <activity android:name=".MainActivity" android:label="" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
在layout下建一個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>
三個類中相關的代碼:
DialogActivity:
package com.example.acticitylifecycletest;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
//註意:這裏不能繼承AppCompatActivity 不然會報錯
public class DialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
}
NormalActivity:
package com.example.acticitylifecycletest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class NormalActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_normal);
}
}
MainActivity:
package com.example.acticitylifecycletest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startNormalActivity = (Button) findViewById(R.id.start_normal_activity);
Button startDialogActivity = (Button) findViewById(R.id.start_dialog_activity);
//正常的按鈕事件
startNormalActivity.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: 重啟");
}
}
2.在銷毀時候傳值
當窗口處於停止狀態的時候,我們需要保存一部分銷毀前的值,可以使用bundle,如下:
mainActicity中加入:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
String template = "something you typed";
outState.putString("data_key",template);
}
在onCreate中取出
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: ");
if (savedInstanceState!=null){
// 取出銷毀時候保存的值
String templateData = savedInstanceState.getString("data_key");
Log.d(TAG, templateData);
}
}
體驗活動的生命周期