activity跳转时的生命周期

概述

研究activity跳转时的生命周期,分完全覆盖的activity跳转,与不完全覆盖的

生命周期



代码


MainActivity

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package test.activitylife;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.AlertDialog.Builder;  
  6. import android.content.DialogInterface;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13.   
  14. public class MainActivity extends Activity {  
  15.       
  16.     private Button onPause;  
  17.     private Button onStop;  
  18.     private static final String TAG = MainActivity.class.getSimpleName();  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.         findViews();  
  24.         onPause.setOnClickListener(new OnClickListener() {  
  25.   
  26.             public void onClick(View v) {  
  27.                 Intent intent = new Intent(MainActivity.this, PauseActivity.class);  
  28.                 startActivity(intent);  
  29.             }  
  30.               
  31.         });  
  32.           
  33.         onStop.setOnClickListener(new OnClickListener(){  
  34.             public void onClick(View v) {  
  35.                 Intent intent = new Intent(MainActivity.this, OtherActivity.class);  
  36.                 startActivity(intent);  
  37.             }  
  38.               
  39.         });  
  40.         Log.v(TAG, "---------------onCreate--------------------");  
  41.     }  
  42.   
  43.     @Override  
  44.     protected void onStart() {  
  45.         Log.v(TAG, "---------------onStart--------------------");  
  46.         super.onStart();  
  47.     }  
  48.   
  49.     @Override  
  50.     protected void onRestart() {  
  51.         Log.v(TAG, "---------------onRestart--------------------");  
  52.         super.onRestart();  
  53.     }  
  54.   
  55.     @Override  
  56.     protected void onResume() {  
  57.         Log.v(TAG, "---------------onResume--------------------");  
  58.         super.onResume();  
  59.     }  
  60.   
  61.     @Override  
  62.     protected void onPause() {  
  63.         Log.v(TAG, "---------------onPause--------------------");  
  64.         super.onPause();  
  65.     }  
  66.   
  67.     @Override  
  68.     protected void onStop() {  
  69.         Log.v(TAG, "---------------onStop--------------------");  
  70.         super.onStop();  
  71.     }  
  72.   
  73.     @Override  
  74.     protected void onDestroy() {  
  75.         Log.v(TAG, "---------------onDestroy--------------------");  
  76.         super.onDestroy();  
  77.     }  
  78.       
  79.     public void findViews() {  
  80.         onPause = (Button)findViewById(R.id.dialog);  
  81.         onStop = (Button)findViewById(R.id.activity);  
  82.     }  
  83.       
  84. }  
OtherActivity

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package test.activitylife;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6.   
  7. public class OtherActivity extends Activity {  
  8.     private static final String TAG=OtherActivity.class.getSimpleName();  
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         Log.v(TAG, "---------------onCreate--------------------");  
  12.         super.onCreate(savedInstanceState);  
  13.           
  14.     }  
  15.     @Override  
  16.     protected void onStart() {  
  17.         // TODO Auto-generated method stub  
  18.         Log.v(TAG, "---------------onStart--------------------");  
  19.         super.onStart();  
  20.     }  
  21.     @Override  
  22.     protected void onRestart() {  
  23.         Log.v(TAG, "---------------onRestart--------------------");  
  24.         super.onRestart();  
  25.     }  
  26.   
  27.     @Override  
  28.     protected void onResume() {  
  29.         Log.v(TAG, "---------------onResume--------------------");  
  30.         super.onResume();  
  31.     }  
  32.   
  33.     @Override  
  34.     protected void onPause() {  
  35.         Log.v(TAG, "---------------onPause--------------------");  
  36.         super.onPause();  
  37.     }  
  38.   
  39.     @Override  
  40.     protected void onStop() {  
  41.         Log.v(TAG, "---------------onStop--------------------");  
  42.         super.onStop();  
  43.     }  
  44.   
  45.     @Override  
  46.     protected void onDestroy() {  
  47.         Log.v(TAG, "---------------onDestroy--------------------");  
  48.         super.onDestroy();  
  49.     }  
  50.       
  51. }  

PauseActivity

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package test.activitylife;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6.   
  7. public class PauseActivity extends Activity {  
  8.   
  9.     private static final String TAG=PauseActivity.class.getSimpleName();  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         // TODO Auto-generated method stub  
  13.         Log.v(TAG, "---------------onCreate--------------------");  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.other);  
  16.     }  
  17.   
  18.     @Override  
  19.     protected void onStart() {  
  20.         Log.v(TAG, "---------------onStart--------------------");  
  21.         super.onStart();  
  22.     }  
  23.     @Override  
  24.     protected void onRestart() {  
  25.         Log.v(TAG, "---------------onRestart--------------------");  
  26.         super.onRestart();  
  27.     }  
  28.   
  29.     @Override  
  30.     protected void onResume() {  
  31.         Log.v(TAG, "---------------onResume--------------------");  
  32.         super.onResume();  
  33.     }  
  34.   
  35.     @Override  
  36.     protected void onPause() {  
  37.         Log.v(TAG, "---------------onPause--------------------");  
  38.         super.onPause();  
  39.     }  
  40.   
  41.     @Override  
  42.     protected void onStop() {  
  43.         Log.v(TAG, "---------------onStop--------------------");  
  44.         super.onStop();  
  45.     }  
  46.   
  47.     @Override  
  48.     protected void onDestroy() {  
  49.         Log.v(TAG, "---------------onDestroy--------------------");  
  50.         super.onDestroy();  
  51.     }  
  52. }  

main.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="hello" />  
  11.     <Button  
  12.         android:id="@+id/dialog"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="OnPause"  
  16.         />  
  17.     <Button  
  18.         android:id="@+id/activity"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="Onstop"  
  22.         />  
  23. </LinearLayout>  

other.xml

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7. </LinearLayout>  

manifest

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     package="test.activitylife"    
  4.     android:versionCode="1"    
  5.     android:versionName="1.0" >    
  6.     
  7.     <uses-sdk android:minSdkVersion="9" />    
  8.     
  9.     <application    
  10.         android:icon="@drawable/ic_launcher"    
  11.         android:label="@string/app_name" >    
  12.         <activity    
  13.             android:name=".MainActivity"    
  14.             android:label="@string/app_name" >    
  15.             <intent-filter>    
  16.                 <action android:name="android.intent.action.MAIN" />    
  17.     
  18.                 <category android:name="android.intent.category.LAUNCHER" />    
  19.             </intent-filter>    
  20.         </activity>    
  21.         <activity android:name=".OtherActivity"></activity>    
  22.         <!-- dialog风格的Activity,没有完全覆盖当前Activity -->    
  23.         <activity android:name=".PauseActivity" android:theme="@android:style/Theme.Dialog"></activity>    
  24.     </application>    
  25.     
  26. </manifest>    

测试结果

OtherActivity完全覆盖MainActivity

点击  OnStop


可以发现在第二个activity的OnResume之后,第一个activity才OnStop,但是在第二个activity的Oncreate之前就执行了OnPause。也就是说只有在第二个activity完全起来之后才调用第一个activity的OnStop

按 返回



PauseActivity不完全覆盖MainActivity


点击 OnPause


这里只调了第一个activity的OnPause,而没有调用OnStop


按 返回

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值