重現onPause到onResume生命周期

jzh2012 7年前發布 | 8K 次閱讀 安卓開發 Android開發 移動開發

最近重看《Android開發藝術探索》一書,其中第3頁 Activity 生命周期 onPause 到 onResume 過程的確如作者所說“在一般的開發中用不上”,但是作為開發者還是有研究的必要。

onResume 的狀態是 Activity 前臺可見正在活動, onPause 是置于前臺可見停止活動。從后者到前者的變化場景,可以通過一個透明的 Dialog 彈出遮蔽 MainActivity 重現。

不過,這是個“特殊”的 Dialog 。一般 Dialog 彈出時,背景可見 MainActivity 依然是 onResume ,表明這個 Activity 的生命周期并沒有因為這個 Dialog 的彈出而變化。

但是,我們使用 Activity 實現一個 Dialog 時,由于是 MainActivity 啟動 DialogActivity ,所以 MainActivity 生命周期必然會變化。

MainActivity.java代碼

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 圖方便直接給findViewById加監聽
        findViewById(R.id.btn_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, DialogActivity.class);
                startActivity(i);
            }
        });
        System.out.println("onCreate");
    }
    // 實現所有生命周期方法Log
    @Override
    protected void onStart() {
        super.onStart();
        System.out.println("onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        System.out.println("onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        System.out.println("onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.out.println("onDestroy");
    }
}

MainActivity.java對應activity_main.xml

Button 只是用來啟動 Dialog_Activity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</RelativeLayout>

DialogActivity.java代碼

public class DialogActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_dialog);
        // 這里圖個方便直接給findViewById加監聽
        findViewById(R.id.returnButton).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                DialogActivity.this.finish();
            }
        });
    }
}

test_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/returnButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="點擊退出"/>

</LinearLayout>

AndroidManifest.xml

注意!!! DialogActivity 使用的主題是 @android:style/Theme.Dialog ,不然上面的代碼就白瞎了

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.phantomvk.exampleapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".DialogActivity"
            android:theme="@android:style/Theme.Dialog"/>
    </application>

</manifest>

下面是結果

// MainActivity到可見可操作的三個周期變化
12-16 23:59:35.806 7477-7477/com.phantomvk.exampleapp I/System.out: onCreate
12-16 23:59:35.811 7477-7477/com.phantomvk.exampleapp I/System.out: onStart
12-16 23:59:35.811 7477-7477/com.phantomvk.exampleapp I/System.out: onResume

// 點擊Button后,MainActivity僅僅是暫停(onPause),沒有去停止(onStop)
12-16 23:59:50.731 7477-7477/com.phantomvk.exampleapp I/System.out: onPause

// 在DialogActivity里面退出,MainActivity狀態變為onResume
12-17 00:00:00.161 7477-7477/com.phantomvk.exampleapp I/System.out: onResume

// 退出應用: onPause -> onStop -> onDestroy
12-17 00:00:30.576 7477-7477/com.phantomvk.exampleapp I/System.out: onPause
12-17 00:00:31.151 7477-7477/com.phantomvk.exampleapp I/System.out: onStop
12-17 00:00:31.151 7477-7477/com.phantomvk.exampleapp I/System.out: onDestroy

上面的代碼已經成功重現 MainActivity 里 onPause -> onResume 。

運行截圖如下(配圖比上面代碼多了TextView,不影響)

提醒一點,直接使用Dialog組件的MainActivity生命周期是不會變化的,這樣應用更加高效且性能更好。這篇文章只是用來重現題目所說的目標,不是用于實踐。

 

來自:http://www.jianshu.com/p/839901db634e

 

 本文由用戶 jzh2012 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!