Activity生命周期,状态保存恢复(经典)

原文链接: http://www.cnblogs.com/james1207/p/3279928.html

一、整体框架

二、main.xml

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

	<Button
        android:id="@+id/startPartActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="startPartActivity" >
    </Button>
    
	<Button
        android:id="@+id/startDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="startDialog" >
    </Button>
    
	<EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />

</LinearLayout>


三、part_activity.xml, 这是一个悬浮Activity的布局。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageView 
    android:id="@+id/iv_logo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/gesture1" 
    /> 
    <TextView  android:layout_width="wrap_content" android:text="Hello Jltxgcy" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/iv_logo" 
    android:textColor="@android:color/black" 
    /> 
</RelativeLayout> 


四、AndroidManifest.xml,悬浮PartActivity需要设置其android:theme属性。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jltxgcy.lifecycledemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="LifecycleDemo" >
        <activity
            android:name=".MainActivity"
            android:label="MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
扫描二维码关注公众号,回复: 6786957 查看本文章


五、MainActivity.java

package com.jltxgcy.lifecycledemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {
	public static final String TAG="jltxgcy";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d(TAG, "MainActivity onCreate");
        
        findViewById(R.id.startPartActivity).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, PartActivity.class);
				startActivity(intent);
				
			}
		});
        
        findViewById(R.id.startDialog).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				final CharSequence[] items = {"Red", "Green", "Blue"};

				//调用dialog
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("Pick a color");
				builder.setItems(items, new DialogInterface.OnClickListener() {
				    public void onClick(DialogInterface dialog, int item) {
				        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
				    }
				});
				AlertDialog alert = builder.create();
				alert.show()  ;
				
			}
		});
    }
    
    @Override
   	protected void onStart() {
   		// TODO Auto-generated method stub
   		Log.d(TAG, "MainActivity onStart");
   		super.onStart();
   	}

   	@Override
   	protected void onResume() {
   		Log.d(TAG, "MainActivity onResume");
   		super.onResume();
   	}

   	@Override
   	protected void onPause() {
   		Log.d(TAG, "MainActivity onPause");
   		super.onPause();
   	}

   	@Override
   	protected void onStop() {
   		Log.d(TAG, "MainActivity onStop");
   		super.onStop();
   	}

   	@Override
   	protected void onDestroy() {
   		Log.d(TAG, "MainActivity onDestroy");
   		super.onDestroy();
   	}

   	@Override
   	protected void onRestart() {
   		Log.d(TAG, "MainActivity onRestart");
   		super.onRestart();
   	}
   	
   	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		Log.d("gcyjltx", "onSaveInstanceState");
	}

}

六、PartActivity.java 悬浮Activity

package com.jltxgcy.lifecycledemo;


import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class PartActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE)  ;
		setContentView(R.layout.part_activity)  ;
	}

}


七、生命周期测试

       当第一次进入MainActivity,结果显示如下:

       

       当启动悬浮PartActivity,结果显示如下:

       

       当按Back键返回MainActivity时,结果显示如下:

       

       当启动一个Dialog,没有结果显示,说明启动Dialog不会影响Activity生命周期。

       当按Home键(相当于打开一个新的Activity),结果显示如下:

      

       当点击图标,再次进入MainActivity,结果显示如下:

      

       当点击Back键退出程序,结果显示如下:

     

       Activity生命周期:

       

八、状态保存测试:

         

        打开MainActivity,在EditText中填入jltxgcy,如下图:

        

        当点击Home键(启动新Activity)或者开启PartActivity时,显示如下:

       

        当点击图标返回MainActivity,此时不经过onCreate(Bundle savedInstanceState),显示结果依然如下:

       

       当点击Home键后,如下图杀死进程。

       

       再次点击回到MainActivity,此时会调用onCreat(),结果显示如下:

   

     

      此时EditText中的jltxgcy也恢复了。

      当点击Back键时,没有调用onSaveInstanceState()。因为如果再进入MainAcitivity,则显示结果如下:

      

      此时EditText中的jltxgcy不能恢复了。


      九、代码后期我会传到github上。


转载于:https://www.cnblogs.com/james1207/p/3279928.html

猜你喜欢

转载自blog.csdn.net/weixin_30338497/article/details/94986422