Activity生命周期的执行流程

Activity的生命周期执行流程:
 ·当Activity第1次被启动:
    ·onCreate()->onStart()->onResume()
 ·当Activity被遮挡后再次显示:

   ·onRestart->onStart()->onResume()

 ·当Activity被遮挡:

    ·onPause()->onStop()
 ·当点击返回(Back)键:
   ·onPause()->onStop()->onDestroy()


具体请参看测试案例:
创建三个Activity
MainActivity
SecondActivity
SmallActivity(小号的Activity,在激活的时候只会遮挡上一个Activity的一部分,即:上一个Activity不会完全被
smallActivity遮挡,所以这种情况上一个Activity不会onStop())

SmallActivity创建的时候要做特殊调整
在配置文件中调整ctivity的主题为:
 android:theme="@android:style/Theme.Translucent.NoTitleBar" 
在布局文件中调整
把根节点调整大小为下面的大小,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="#207044"
    tools:context=".SmallActivity" >


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="SmallActivity"
        android:textSize="30sp" />


</RelativeLayout>


MainActivity:

package com.example.activity_test;

import android.location.GpsStatus.Listener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {
	private final String TAG = "Activity LifeCycle";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d(TAG, "MainActivity -> onCreate()");
		InnerOnClickListener listener = new InnerOnClickListener();
		findViewById(R.id.btn_SecondActivity).setOnClickListener(listener);
		findViewById(R.id.btn_Small).setOnClickListener(listener);


	}

	private class InnerOnClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_SecondActivity:
				startActivity(new Intent(getApplicationContext(),SecondActivity.class));

				break;
			case R.id.btn_Small:
				startActivity(new Intent(getApplicationContext(),SmallActivity.class));
				break;

			default:
				break;
			}

		}

	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.d(TAG, "MainActivity -> onStart()");
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d(TAG, "MainActivity -> onResume()");
	}
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.d(TAG, "MainActivity -> onRestart()");
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.d(TAG, "MainActivity -> onPause()");
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.d(TAG, "MainActivity -> onStop()");
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d(TAG, "MainActivity -> onDestroy()");
	}



}

SecondActivity:

package com.example.activity_test;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class SecondActivity extends Activity {
	private final String TAG = "Activity LifeCycle";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		Log.e(TAG, "SecondActivity -> onCreate()");
	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.e(TAG, "SecondActivity -> onStart()");
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.e(TAG, "SecondActivity -> onResume()");
	}
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.e(TAG, "SecondActivity -> onRestart()");
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.e(TAG, "SecondActivity -> onPause()");
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.e(TAG, "SecondActivity -> onStop()");
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.e(TAG, "SecondActivity -> onDestroy()");
	}
}

SmallActivity:

package com.example.activity_test;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class SmallActivity extends Activity {
	private final String TAG = "Activity LifeCycle";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_small);
		Log.i(TAG, "SmallActivity -> onCreate()");
	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.i(TAG, "SmallActivity -> onStart()");
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.i(TAG, "SmallActivity -> onResume()");
	}
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.i(TAG, "SmallActivity -> onRestart() ");
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.i(TAG, "SmallActivity -> onPause()");
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.i(TAG, "SmallActivity ->  onStop()");
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i(TAG, "SmallActivity -> onDestroy()");
	}
}


你可能感兴趣的:(android)