`

Activity生命周期

 
阅读更多
Activity1

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Activity1 extends Activity {
	
	private Button nextButton;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	System.out.println(this.getClass().getName() + "====onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        nextButton = (Button) this.findViewById(R.id.nextButton);
        nextButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent it = new Intent();
				it.setClass(Activity1.this, Activity2.class);
				Activity1.this.startActivity(it);
				
			}
        	
        });
    }

	@Override
	protected void onStart() {
		System.out.println(this.getClass().getName() + "====onStart");
		super.onStart();
	}

	@Override
	protected void onRestart() {
		System.out.println(this.getClass().getName() + "====onRestart");
		super.onRestart();
	}

	@Override
	protected void onResume() {
		System.out.println(this.getClass().getName() + "====onResume");
		super.onResume();
	}

	@Override
	protected void onPause() {
		System.out.println(this.getClass().getName() + "====onPause");
		super.onPause();
	}

	@Override
	protected void onStop() {
		System.out.println(this.getClass().getName() + "====onStop");
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		System.out.println(this.getClass().getName() + "====onDestroy");
		super.onDestroy();
	}
    
    
}



Activity2
public class Activity2 extends Activity {
	
	private Button nextButton;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	System.out.println(this.getClass().getName() + "====onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);       
    }

	@Override
	protected void onStart() {
		System.out.println(this.getClass().getName() + "====onStart");
		super.onStart();
	}

	@Override
	protected void onRestart() {
		System.out.println(this.getClass().getName() + "====onRestart");
		super.onRestart();
	}

	@Override
	protected void onResume() {
		System.out.println(this.getClass().getName() + "====onResume");
		super.onResume();
	}

	@Override
	protected void onPause() {
		System.out.println(this.getClass().getName() + "====onPause");
		super.onPause();
	}

	@Override
	protected void onStop() {
		System.out.println(this.getClass().getName() + "====onStop");
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		System.out.println(this.getClass().getName() + "====onDestroy");
		super.onDestroy();
	}
    
    
}



AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.demo"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Activity1"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name=".Activity2"
                  android:label="@string/app_name2">
            
        </activity>

    </application>
</manifest>


log

    //启动Activity1
    11-01 03:04:39.877: INFO/System.out(305): com.demo.Activity1====onCreate
    11-01 03:04:40.008: INFO/System.out(305): com.demo.Activity1====onStart
    11-01 03:04:40.008: INFO/System.out(305): com.demo.Activity1====onResume
    //点击按钮到Activity2
    11-01 03:05:12.678: INFO/System.out(305): com.demo.Activity1====onPause
    11-01 03:05:12.768: INFO/System.out(305): com.demo.Activity2====onCreate
    11-01 03:05:12.809: INFO/System.out(305): com.demo.Activity2====onStart
    11-01 03:05:12.818: INFO/System.out(305): com.demo.Activity2====onResume
    11-01 03:05:13.348: INFO/System.out(305): com.demo.Activity1====onStop
    //按键盘上的返回Activity1
    11-01 03:06:01.088: INFO/System.out(305): com.demo.Activity2====onPause
    11-01 03:06:01.138: INFO/System.out(305): com.demo.Activity1====onRestart
    11-01 03:06:01.148: INFO/System.out(305): com.demo.Activity1====onStart
    11-01 03:06:01.148: INFO/System.out(305): com.demo.Activity1====onResume
    11-01 03:06:01.568: INFO/System.out(305): com.demo.Activity2====onStop
    11-01 03:06:01.568: INFO/System.out(305): com.demo.Activity2====onDestroy




弹出Activity时, 第一个activity没有执行onStop()

 <activity android:name=".Activity2"
                  android:label="@string/app_name2"
                  android:theme="@android:style/Theme.Dialog">
            
        </activity>


log
引用
11-01 05:56:04.557: INFO/System.out(220): com.demo.Activity1====onCreate
11-01 05:56:04.799: INFO/System.out(220): com.demo.Activity1====onStart
11-01 05:56:04.818: INFO/System.out(220): com.demo.Activity1====onResume
11-01 05:56:36.918: INFO/System.out(220): com.demo.Activity1====onPause
11-01 05:56:36.999: INFO/System.out(220): com.demo.Activity2====onCreate
11-01 05:56:37.038: INFO/System.out(220): com.demo.Activity2====onStart
11-01 05:56:37.038: INFO/System.out(220): com.demo.Activity2====onResume

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics