Activity的生命周期
来源:互联网 发布:盈建科软件最新版下载 编辑:程序博客网 时间:2024/06/12 23:58
Activity的生命周期
![先看一张经典的生命周期图](http://img.blog.csdn.net/20160610150434651)
- OnCreate()
- 在Activity第一次创建启动时调用
- OnStart()
- 当前的Activity由不可见为成可见时调用,例如按下Home键退出Activity或者当前的Activity被其他Activity彻底覆盖时调用
- OnResume()
- 当前的Activity位于状态,处在运行状态,和用户交互时,所调用
- OnPause()
- 当前的Activity被遮挡住,但仍然为可见状态时,会进入暂停状态,会执行此方法,当遮挡物消失时,会执行OnResume(),再次进入运行状态.
- OnStop()
-当前的Activity彻底为不可见时,,例如按下home键,会先调用OnPause,在接着调用OnStop进入停止状态。 - OnRestart()
-当Activity为彻底可见时,就会调用此方法,再调用OnStart,在调用OnResume; - OnDestroy()
-当按下OnBack键,就会执行OnDestroy() ,销毁当前Activity
activity_main.xml<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/btn_Normal" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This a is Dlialog"/> <Button android:id="@+id/btn_Dlialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This a is Normal"/></LinearLayout>nomal_activity.xml<RelativeLayout 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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this a is nomal activity" /></RelativeLayout>dialog_activity.xml<RelativeLayout 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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this a is dialog activity" /></RelativeLayout>public class MainActivity extends Activity implements OnClickListener{ private Button mBtnNormal,mBtnDliaog; private static String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(savedInstanceState!=null) { String value = savedInstanceState.getString("strValue"); } mBtnNormal = (Button) findViewById(R.id.btn_Normal); mBtnDliaog = (Button) findViewById(R.id.btn_Dlialog); mBtnNormal.setOnClickListener(this); mBtnDliaog.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_Normal: Intent intent = new Intent(this,NomalActivity.class); startActivity(intent); break; case R.id.btn_Dlialog: Intent intent2 = new Intent(this,DialogActivty.class); startActivity(intent2); //ActivityCollector.Activityfinish(); break; } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); String str = "Hellow Android"; outState.putString("strValue", str); } @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 onRestart() { super.onRestart(); Log.d(TAG, "onRestart()"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy()"); }}public class DialogActivty extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_activity); }}public class NomalActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.nomal_activity); }}AndroidManifest.xml<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <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=".NomalActivity"></activity> <activity android:name=".DialogActivty" android:theme="@android:style/Theme.Dialog"></activity></application> mBtnNormal ![点击mBtnNormal](http://img.blog.csdn.net/20160610154919936) ![按下Back键盘](http://img.blog.csdn.net/20160610155020858) mBtnDliaog ![点击mBtnDliaog](http://img.blog.csdn.net/20160610155223405) ![按下Back键盘](http://img.blog.csdn.net/20160610155250374)
0 0
- 什么是Activity?Activity的生命周期!
- Activity及Activity的生命周期
- Activity 的生命周期
- Android activity的生命周期
- Activity的生命周期
- Android:Activity的生命周期
- Activity的生命周期
- Activity的生命周期
- Activity的生命周期
- Activity的生命周期
- Activity的生命周期
- Activity的生命周期
- Android Activity的生命周期
- Android Activity的生命周期
- Activity的生命周期一
- Android Activity的生命周期
- Activity的生命周期
- Android activity的生命周期
- Android SQLiteDatabase使用详细教程
- LightOJ 1405 The Great Escape 最大流
- 【C】少年时代的经典游戏:三子棋
- 闭包的一个注意事项
- [Alfred]替代Spotlight
- Activity的生命周期
- VC中ADO Data控件的使用
- hive 入门
- Java 直接插入排序
- 设计模式 II ——策略模式(Strategy)
- Exchange 2010 Ps 之使用更新脱机地址簿系列命令get-offlineaddressbook&Update-offlineaddressbook
- tomcat 内存溢出解决方案
- hive-0
- Maven: Failed to retrieve plugin descriptor error