Android组件通信(二十四)

1. Activity生命周期

1.1 知识点

(1)掌握Activity的生命周期及操作方法;

1.2 具体内容

Android组件通信(二十四)_第1张图片

Android组件通信(二十四)_第2张图片

Android组件通信(二十四)_第3张图片

范例:

第一个配置文件



    
    

第二个配置文件:



    
    

第三个配置文件:



    
   



第一个Activity

package com.example.activitylifeproject;

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

public class ActivityLifeActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity_life_first);
		Log.i("sign","第一个Activity的onCreate方法");
		Button but = (Button) super.findViewById(R.id.but_first);
		but.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent it = new Intent(ActivityLifeActivity.this,ActivityLifeSecondActivity.class);
				startActivity(it);
			}
		});
	}
    @Override
    protected void onStart(){
    	Log.i("sign","第一个Activity的onStart方法");
    	super.onStart();
    }
    @Override
    protected void onResume(){
    	Log.i("sign","第一个Activity的onResume方法");
    	super.onResume();
    }
    @Override
    protected void onPause(){
    	Log.i("sign","第一个Activity的onPause方法");
    	super.onPause();
    }
    @Override
    protected void onStop(){
    	Log.i("sign","第一个Activity的onStop方法");
    	super.onStop();
    }
    @Override
    protected void onDestroy(){
    	Log.i("sign","第一个Activity的onDestroy方法");
    	super.onDestroy();
    }
    @Override
    protected void onRestart(){
    	Log.i("sign","第一个Activity的onRestart方法");
    	super.onRestart();
    }
}

第二个Activity

package com.example.activitylifeproject;

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;
import android.widget.Button;

public class ActivityLifeSecondActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity_life_second);
		Log.i("sign","第二个Activity的onCreate方法");
		Button but = (Button) super.findViewById(R.id.but_second);
		but.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent it = new Intent(ActivityLifeSecondActivity.this,ActivityLifeThirdActivity.class);
				startActivity(it);
			}
		});
	}

	@Override
    protected void onStart(){
    	Log.i("sign","第二个Activity的onStart方法");
    	super.onStart();
    }
    @Override
    protected void onResume(){
    	Log.i("sign","第二个Activity的onResume方法");
    	super.onResume();
    }
    @Override
    protected void onPause(){
    	Log.i("sign","第二个Activity的onPause方法");
    	super.onPause();
    }
    @Override
    protected void onStop(){
    	Log.i("sign","第二个Activity的onStop方法");
    	super.onStop();
    }
    @Override
    protected void onDestroy(){
    	Log.i("sign","第二个Activity的onDestroy方法");
    	super.onDestroy();
    }
    @Override
    protected void onRestart(){
    	Log.i("sign","第二个Activity的onRestart方法");
    	super.onRestart();
    }
}

第三个Activity

package com.example.activitylifeproject;

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

public class ActivityLifeThirdActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity_life_third);
	}

	@Override
    protected void onStart(){
    	Log.i("sign","第三个Activity的onStart方法");
    	super.onStart();
    }
    @Override
    protected void onResume(){
    	Log.i("sign","第三个Activity的onResume方法");
    	super.onResume();
    }
    @Override
    protected void onPause(){
    	Log.i("sign","第三个Activity的onPause方法");
    	super.onPause();
    }
    @Override
    protected void onStop(){
    	Log.i("sign","第三个Activity的onStop方法");
    	super.onStop();
    }
    @Override
    protected void onDestroy(){
    	Log.i("sign","第三个Activity的onDestroy方法");
    	super.onDestroy();
    }
    @Override
    protected void onRestart(){
    	Log.i("sign","第三个Activity的onRestart方法");
    	super.onRestart();
    }

}

1.3 小结

(1)Activity提供了专门的生命周期方法;

(2)在多个Activity打开的时候,会自动的进行入栈操作;

你可能感兴趣的:(Android,开发语言,android)