演示Activity的生命周期

Activity的生命周期:


演示Activity的生命周期_第1张图片


演示过程:(1)MainActivity和Activity2之间的代码

Activity2的代码:                                                                                                                      

package com.example.mactivitytest;                                         
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;


public class MainActivity extends Activity {
private Button btn1,btn2;
//Activity创建时被调用 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Log.d("MainActivity", "onCreate");
btn1=(Button) findViewById(R.id.btn1);
btn2=(Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(getApplicationContext(),Activity2.class);
startActivity(intent);
// MainActivity.this.finish();
}
});
btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
MainActivity.this.finish();
}
});




}
//Activity创建或者从后台重新回到前台时被调用  
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d("MainActivity", "onStart");


}
//Activity创建或者从被覆盖、后台重新回到前台时被调用
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d("MainActivity", "onResume");
}
//Activity被覆盖到下面或者锁屏时被调用 
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d("MainActivity", "onPause");
}
 //退出当前Activity或者跳转到新Activity时被调用 
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d("MainActivity", "onStop");


}
//退出当前Activity时被调用,调用之后Activity就结束了 
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("MainActivity", "onDestroy");
}
}

  mainActivity的代码:

package com.example.mactivitytest;
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 Activity2 extends Activity {
private Button btn3, btn4;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Activity2", "onCreate");
btn3=(Button) findViewById(R.id.btn3);
btn4=(Button) findViewById(R.id.btn4);
btn3.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
// Activity2.this.finish();
}
});
btn4.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Activity2.this.finish();
}
});


}


@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d("Activity2", "onStart");


}


@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d("Activity2", "onResume");
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d("Activity2", "onPause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d("Activity2", "onStop");


}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("Activity2", "onDestroy");
}
}

布局activity_main:     xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/test2" />


   
布局fragment_main:

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


            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/test" />


            android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="切换到第二个activity" />


            android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="关闭" />



演示效果:(1)运行项目

在系统调用了onCreate和onStart之后,调用了onResume,自此,Activity进入了运行状态。

(2)点击切换到第一个Activity按钮


在启动Activity2调用Activity2的oncreate,onstart,oonResume.让MainACtivity处于onstop状态中。

(3)点击切换到第二个Activity按钮


调用Activity2的onpause.重新调用MainActivity的oncreate,onstaart,onResume.让Activity处于onstop的状态中。

我就演示到这。其他点击可以自由组合,观察生命周期。

mActivityTest源码链接:http://pan.baidu.com/s/1kVua3xd

你可能感兴趣的:(移动开发)