经典的生命周期流程图:
相信不少朋友也已经看过这个流程图了,也基本了解了Activity生命周期的几个过程,我们就来说一说这几个过程。
1.启动Activity:系统会先调用onCreate方法,然后调用onStart方法,最后调用onResume,Activity进入运行状态。
2.当前Activity被其他Activity覆盖其上或被锁屏:系统会调用onPause方法,暂停当前Activity的执行。
3.当前Activity由被覆盖状态回到前台或解锁屏:系统会调用onResume方法,再次进入运行状态。
4.当前Activity转到新的Activity界面或按Home键回到主屏,自身退居后台:系统会先调用onPause方法,然后调用onStop方法,进入停滞状态。
5.用户后退回到此Activity:系统会先调用onRestart方法,然后调用onStart方法,最后调用onResume方法,再次进入运行状态。
6.当前Activity处于被覆盖状态或者后台不可见状态,即第2步和第4步,系统内存不足,杀死当前Activity,而后用户退回当前Activity:再次调用onCreate方法、onStart方法、onResume方法,进入运行状态。
7.用户退出当前Activity:系统先调用onPause方法,然后调用onStop方法,最后调用onDestory方法,结束当前Activity。
实例
1.新建项目<IntentputExtra>
2.在layout目录下,新建normal_layout.xml,并添加文本
3.在layout目录下,新建dialog_layout.xml,并添加对话框文本
4.在Java目录下,新建NormalActivity.java中,加载normal_layout布局
5.在Java目录下,新建DialogActivity.java,并加载dialog_layout布局,要做成对话框式的,需要在AndroidManifest.xml下,修改主题类型
6.在AndroidManifest.xml下,MainActivity自动生成,作为主活动 。注册2个新建的活动,并修改DialogActivity的主题类型
7.在layout目录下,修改activity_main.xml,重新定制主活动的布局
8.在java目录下,修改MainActivity的代码,能够在日志中显示出MainAtivity的活动状态
代码如下:
layout——>normal_layout.xml (2)
<TextView
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "This is a normal activity" />
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "This is a normal activity" />
layout——>dialog_layout.xml (3)
<TextView
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "This is a dialog activity" />
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "This is a dialog activity" />
Java——>NormalActivity.java (4)
public class NormalActivity
extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState) ;
requestWindowFeature(Window. FEATURE_NO_TITLE) ;
setContentView(R.layout. normal_layout) ;
}
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState) ;
requestWindowFeature(Window. FEATURE_NO_TITLE) ;
setContentView(R.layout. normal_layout) ;
}
}
Java——>DialogActivity.java (5)
public class DialogActivity
extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState) ;
requestWindowFeature(Window. FEATURE_NO_TITLE) ;
setContentView(R.layout. dialog_layout) ;
}
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState) ;
requestWindowFeature(Window. FEATURE_NO_TITLE) ;
setContentView(R.layout. dialog_layout) ;
}
}
manifests——> AndroidManifest.xml(6)
<activity
android
:name=
".NormalActivity"
></activity>
<activity android :name= ".DialogActivity"
android :theme= "@android:style/Theme.Dialog" ></activity>
<activity android :name= ".DialogActivity"
android :theme= "@android:style/Theme.Dialog" ></activity>
layout——>activity_main.xml (7)
<LinearLayout
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 :orientation= "vertical" >
<Button
android :id= "@+id/start_normal_activity"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "Start NormalActivity" />
<Button
android :id= "@+id/start_dialog_activity"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "Start DialogActivity" />
</LinearLayout>
xmlns: tools = "http://schemas.android.com/tools"
android :layout_width= "match_parent"
android : layout_height = "match_parent"
android :orientation= "vertical" >
<Button
android :id= "@+id/start_normal_activity"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "Start NormalActivity" />
<Button
android :id= "@+id/start_dialog_activity"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "Start DialogActivity" />
</LinearLayout>
Java——>MainActivity .java (8)
public class MainActivity
extends AppCompatActivity {
public static final String TAG = "MainActivity" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
Log. d( TAG , "onCreate") ;
requestWindowFeature(Window. FEATURE_NO_TITLE) ;
setContentView(R.layout. activity_main) ;
Button startNormalActivity=(Button) findViewById(R.id. start_normal_activity) ;
Button startDialogActivity=(Button) findViewById(R.id. start_dialog_activity) ;
startNormalActivity.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity. this,NormalActivity. class) ;
startActivity(intent) ;
}
}) ;
startDialogActivity.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(MainActivity. this,DialogActivity. class) ;
startActivity(intent) ;
}
}) ;
}
@Override
protected void onStart(){
super.onStart() ;
Log. d( TAG , "onStart") ;
}
@Override
protected void onResume(){
super.onResume() ;
Log. d( TAG , "onResume") ;
}
@Override
protected void onPause(){
super.onPause() ;
Log. d( TAG , "onPause") ;
}
@Override
protected void onStop(){
super.onStop() ;
Log. d( TAG , "onStop") ;
}
@Override
protected void onDestroy(){
super.onDestroy() ;
Log. d( TAG , "onDestroy") ;
}
@Override
protected void onRestart(){
super.onRestart() ;
Log. d( TAG , "onRestart") ;
}
}
public static final String TAG = "MainActivity" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
Log. d( TAG , "onCreate") ;
requestWindowFeature(Window. FEATURE_NO_TITLE) ;
setContentView(R.layout. activity_main) ;
Button startNormalActivity=(Button) findViewById(R.id. start_normal_activity) ;
Button startDialogActivity=(Button) findViewById(R.id. start_dialog_activity) ;
startNormalActivity.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity. this,NormalActivity. class) ;
startActivity(intent) ;
}
}) ;
startDialogActivity.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(MainActivity. this,DialogActivity. class) ;
startActivity(intent) ;
}
}) ;
}
@Override
protected void onStart(){
super.onStart() ;
Log. d( TAG , "onStart") ;
}
@Override
protected void onResume(){
super.onResume() ;
Log. d( TAG , "onResume") ;
}
@Override
protected void onPause(){
super.onPause() ;
Log. d( TAG , "onPause") ;
}
@Override
protected void onStop(){
super.onStop() ;
Log. d( TAG , "onStop") ;
}
@Override
protected void onDestroy(){
super.onDestroy() ;
Log. d( TAG , "onDestroy") ;
}
@Override
protected void onRestart(){
super.onRestart() ;
Log. d( TAG , "onRestart") ;
}
}
代码运行结果如下:
开始运行:
点击第一个按钮:
按返回,返回主界面:
点击第二个按钮:
按返回,返回主界面: