activity跳转时的生命周期
来源:互联网 发布:数据库中的模式 编辑:程序博客网 时间:2024/04/30 20:17
概述
研究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--------------------"); }@Overrideprotected void onStart() {Log.v(TAG, "---------------onStart--------------------");super.onStart();}@Overrideprotected void onRestart() {Log.v(TAG, "---------------onRestart--------------------");super.onRestart();}@Overrideprotected void onResume() {Log.v(TAG, "---------------onResume--------------------");super.onResume();}@Overrideprotected void onPause() {Log.v(TAG, "---------------onPause--------------------");super.onPause();}@Overrideprotected void onStop() {Log.v(TAG, "---------------onStop--------------------");super.onStop();}@Overrideprotected 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();@Overrideprotected void onCreate(Bundle savedInstanceState) {Log.v(TAG, "---------------onCreate--------------------");super.onCreate(savedInstanceState);}@Overrideprotected void onStart() {// TODO Auto-generated method stubLog.v(TAG, "---------------onStart--------------------");super.onStart();}@Overrideprotected void onRestart() {Log.v(TAG, "---------------onRestart--------------------");super.onRestart();}@Overrideprotected void onResume() {Log.v(TAG, "---------------onResume--------------------");super.onResume();}@Overrideprotected void onPause() {Log.v(TAG, "---------------onPause--------------------");super.onPause();}@Overrideprotected void onStop() {Log.v(TAG, "---------------onStop--------------------");super.onStop();}@Overrideprotected 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();@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubLog.v(TAG, "---------------onCreate--------------------");super.onCreate(savedInstanceState);setContentView(R.layout.other);}@Overrideprotected void onStart() {Log.v(TAG, "---------------onStart--------------------");super.onStart();}@Overrideprotected void onRestart() {Log.v(TAG, "---------------onRestart--------------------");super.onRestart();}@Overrideprotected void onResume() {Log.v(TAG, "---------------onResume--------------------");super.onResume();}@Overrideprotected void onPause() {Log.v(TAG, "---------------onPause--------------------");super.onPause();}@Overrideprotected void onStop() {Log.v(TAG, "---------------onStop--------------------");super.onStop();}@Overrideprotected 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
按 返回
参考资料
http://blog.csdn.net/android_tutor/article/details/5772285
2 0
- activity跳转时的生命周期
- activity跳转时的生命周期
- activity跳转时的生命周期
- Activity 跳转时的生命周期流程
- Activity的生命周期,activity跳转到activity
- activity跳转的生命周期调用
- Activity跳转时生命周期跟踪
- 两个Activity之间跳转时,生命周期的执行顺序
- 两个Activity跳转时 生命周期函数的变化
- Android面试题库---Activity之间跳转时的生命周期变换
- Android开发艺术探索学习-Activity跳转时的生命周期
- 两个Activity之间跳转时,它们生命周期的执行顺序
- 两个Activity之间跳转时,生命周期的执行顺序
- 两个Activity之间跳转时,生命周期的执行顺序
- 两个Activity之间跳转执行的生命周期
- activity跳转生命周期
- Activity跳转生命周期变化
- activity 跳转生命周期研究
- 画线
- equals方法及对其进行重写
- poj 3262 Protecting the Flowers (贪心)
- 关于学java要掌握的知识问题
- Zigbee 设置信道,PANID,发射功率
- activity跳转时的生命周期
- 分享几个免费Cocos2d-x游戏源码
- IOS 多语言本地化 完美解决方案【自动+手动】
- 使用pt-stalk诊断MySQL问题
- VMware-vSphere-SDK-5.0 实现快照还原(一)获取最新快照名称
- ANT简明教程
- JAVA交通灯
- poj3134 Power Calculus---迭代加深dfs
- 前台学习网址