activity跳转时的生命周期
2014-01-09 11:34
267 查看
概述
研究activity跳转时的生命周期,分完全覆盖的activity跳转,与不完全覆盖的源代码 http://download.csdn.net/detail/yuxmdef1/6829217
生命周期
代码
MainActivity
package test.activitylife; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; 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 MainActivity extends Activity { private Button onPause; private Button onStop; private static final String TAG = MainActivity.class.getSimpleName(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); onPause.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainActivity.this, PauseActivity.class); startActivity(intent); } }); onStop.setOnClickListener(new OnClickListener(){ public void onClick(View v) { Intent intent = new Intent(MainActivity.this, OtherActivity.class); startActivity(intent); } }); Log.v(TAG, "---------------onCreate--------------------"); } @Override protected void onStart() { Log.v(TAG, "---------------onStart--------------------"); super.onStart(); } @Override protected void onRestart() { Log.v(TAG, "---------------onRestart--------------------"); super.onRestart(); } @Override protected void onResume() { Log.v(TAG, "---------------onResume--------------------"); super.onResume(); } @Override protected void onPause() { Log.v(TAG, "---------------onPause--------------------"); super.onPause(); } @Override protected void onStop() { Log.v(TAG, "---------------onStop--------------------"); super.onStop(); } @Override protected void onDestroy() { Log.v(TAG, "---------------onDestroy--------------------"); super.onDestroy(); } public void findViews() { onPause = (Button)findViewById(R.id.dialog); onStop = (Button)findViewById(R.id.activity); } }OtherActivity
package test.activitylife; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class OtherActivity extends Activity { private static final String TAG=OtherActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { Log.v(TAG, "---------------onCreate--------------------"); super.onCreate(savedInstanceState); } @Override protected void onStart() { // TODO Auto-generated method stub Log.v(TAG, "---------------onStart--------------------"); super.onStart(); } @Override protected void onRestart() { Log.v(TAG, "---------------onRestart--------------------"); super.onRestart(); } @Override protected void onResume() { Log.v(TAG, "---------------onResume--------------------"); super.onResume(); } @Override protected void onPause() { Log.v(TAG, "---------------onPause--------------------"); super.onPause(); } @Override protected void onStop() { Log.v(TAG, "---------------onStop--------------------"); super.onStop(); } @Override protected void onDestroy() { Log.v(TAG, "---------------onDestroy--------------------"); super.onDestroy(); } }
PauseActivity
package test.activitylife; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class PauseActivity extends Activity { private static final String TAG=PauseActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub Log.v(TAG, "---------------onCreate--------------------"); super.onCreate(savedInstanceState); setContentView(R.layout.other); } @Override protected void onStart() { Log.v(TAG, "---------------onStart--------------------"); super.onStart(); } @Override protected void onRestart() { Log.v(TAG, "---------------onRestart--------------------"); super.onRestart(); } @Override protected void onResume() { Log.v(TAG, "---------------onResume--------------------"); super.onResume(); } @Override protected void onPause() { Log.v(TAG, "---------------onPause--------------------"); super.onPause(); } @Override protected void onStop() { Log.v(TAG, "---------------onStop--------------------"); super.onStop(); } @Override protected void onDestroy() { Log.v(TAG, "---------------onDestroy--------------------"); super.onDestroy(); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="hello" /> <Button android:id="@+id/dialog" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="OnPause" /> <Button android:id="@+id/activity" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Onstop" /> </LinearLayout>
other.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout>
manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.activitylife" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="9" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".OtherActivity"></activity> <!-- dialog风格的Activity,没有完全覆盖当前Activity --> <activity android:name=".PauseActivity" android:theme="@android:style/Theme.Dialog"></activity> </application> </manifest>
测试结果
OtherActivity完全覆盖MainActivity
点击 OnStop可以发现在第二个activity的OnResume之后,第一个activity才OnStop,但是在第二个activity的Oncreate之前就执行了OnPause。也就是说只有在第二个activity完全起来之后才调用第一个activity的OnStop
按 返回
PauseActivity不完全覆盖MainActivity
点击 OnPause这里只调了第一个activity的OnPause,而没有调用OnStop
按 返回
参考资料
/article/1456352.html相关文章推荐
- Activity的生命周期及A跳转B的生命周期
- android tabhost下嵌套两个activity(不妨设A和B),其中B包含SurfaceView控件,A可以跳转到C(也是Activity),B会对C的生命周期有奇怪的影响,从何解决?
- activity跳转生命周期
- Android Activity ABC页面跳转生命周期变化及activity + fragment生命周期变化(经典面试题)
- Andriod 学习——Activity之间的跳转,以及Activity的生命周期。
- Activity的跳转、横竖屏切换的生命周期
- 实验三 Task 打电话界面跳转 实验四 Activity的生命周期
- activity跳转的生命周期调用
- 横竖屏切换Activity和activity跳转时生命周期的流程
- 两个Activity跳转时 生命周期函数的变化
- android activity跳转生命周期执行顺序 原理
- 拆解Activity的启动和跳转另一个Activity的生命周期的变化
- activity跳转时的生命周期
- Android之路 1.activity跳转、传值、生命周期、活动
- Android之Activity的生命周期和Activity间的跳转和数据传递
- Android之Activity的生命周期和Activity间的跳转和数据传递
- Activity跳转生命周期变化--SingleTask模式
- 两个Activity之间跳转时,生命周期的执行顺序
- Android Activity生命周期一探究竟 及 主Activity里带Thread跳转时的注意事项
- Activity入门--“生命周期”,启动模式,Intent跳转Flag方式