1. 程式人生 > >體驗活動的生命週期

體驗活動的生命週期

體驗活動的生命週期

Android活動的生命週期圖如下:

shengming.jpg

專案步驟:

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

        }
    }