Activity的生命周期(五)——Activity生命周期的应用场景

通过音乐的播放、暂停、继续播放来表现Activity生命周期的应用

public class UseActivityLife extends Activity implements OnClickListener{

	private String TAG = "UseActivityLife";
	
	private MediaPlayer music;
	private int positon;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_many_a);
		Button btn_switchToB = (Button) findViewById(R.id.btn_switchToB);
		btn_switchToB.setOnClickListener(this);
		Log.e(TAG, "onCreate");
		music = MediaPlayer.create(this, R.raw.beyond);
		music.start();
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		Log.e(TAG, "onStart");
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		Log.e(TAG, "onRestart");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.e(TAG, "onResume");
		if(positon != 0){
			music.seekTo(positon);
			music.start();
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Log.e(TAG, "onPause");
		if(music.isPlaying()){
			music.pause();
			positon = music.getCurrentPosition();
		}
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		Log.e(TAG, "onStop");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.e(TAG, "onDestroy");
		if(music != null){
			music.release();
			music = null;
		}
	}

	@Override
	public void onClick(View v) {
		startActivity(new Intent(UseActivityLife.this, ManyActivity_B.class));
	}
}

public class ActivityTransValue_B extends Activity{

	private TextView tv_value;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_transvalue_b);
		tv_value = (TextView) findViewById(R.id.tv_value);
		
		Intent intent = getIntent();
		if(intent != null){
			String name = intent.getStringExtra("name");
			int age = intent.getIntExtra("age",0);
			tv_value.setText("intent.putExtra传值:  "+"name:"+name+"——"+"age:"+age);
		}
	}
	
}

activity_many_a.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.imooc.activitystudy.OneActivity" >

    <TextView
        android:id="@+id/tv_act"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity_A" />
    <Button 
        android:layout_below="@id/tv_act"
        android:id="@+id/btn_switchToB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳转到Activity_B" />

</RelativeLayout>

activity_transvalue_b.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.imooc.activitystudy.OneActivity" >

    <TextView
        android:id="@+id/tv_value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <ImageView 
        android:layout_below="@id/tv_value"
        android:id="@+id/im_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>


你可能感兴趣的:(Activity的生命周期(五)——Activity生命周期的应用场景)