android学习笔记13--------------Activity生命周期
2012-03-15 00:00
549 查看
Activity除了创建(onCreate)和销毁(onDestroy),还有三个状态:
1.当它在屏幕前台时(位于当前任务堆栈顶部),它是激活或运行状态。它就是响应用户操作的Activity.
2.当它失去焦点但仍对用户可见,它处于暂停状态。
此时的MainActivity处于暂停状态,即在它之上另有一个Activity,这个Activity也许是透明的,或者没有覆盖全屏,如这个对话框,所以
被暂停的MainActivity对用户仍然是可见的。暂停的Activity仍然是存活状态,但系统假如处于极低内存时是可以杀死暂停状态的Activity
的。
3.当完全被另一个Activity覆盖时,则处于停止状态。它对用户是不可见的,例如当MainActivity激活状态时,有电话打入,则MainActivity
处于停止状态,如果其他地方需要内存,则系统可能杀死停止的Activity。
当Activity由一种状态转为另一种状态,会调用以下方法来通知这些变化:
其过程如图:
main.xml
second.xml
AndroidManifest.xml
$(document).ready(function(){dp.SyntaxHighlighter.HighlightAll('code');});
原文链接:
http://blog.csdn.net/yf210yf/article/details/6861481
1.当它在屏幕前台时(位于当前任务堆栈顶部),它是激活或运行状态。它就是响应用户操作的Activity.
2.当它失去焦点但仍对用户可见,它处于暂停状态。
此时的MainActivity处于暂停状态,即在它之上另有一个Activity,这个Activity也许是透明的,或者没有覆盖全屏,如这个对话框,所以
被暂停的MainActivity对用户仍然是可见的。暂停的Activity仍然是存活状态,但系统假如处于极低内存时是可以杀死暂停状态的Activity
的。
3.当完全被另一个Activity覆盖时,则处于停止状态。它对用户是不可见的,例如当MainActivity激活状态时,有电话打入,则MainActivity
处于停止状态,如果其他地方需要内存,则系统可能杀死停止的Activity。
当Activity由一种状态转为另一种状态,会调用以下方法来通知这些变化:
@Override protected void onStart() { super.onStart(); } @Override protected void onRestart() { super.onRestart(); } @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); } @Override protected void onStop() { super.onStop(); } @Override protected void onDestroy() { super.onDestroy(); }
其过程如图:
import android.app.Activity; 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 { Button dailogbButton; Button exitbButton; String Tag="MainActivity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dailogbButton=(Button)findViewById(R.id.dialog); exitbButton=(Button)findViewById(R.id.exit); dailogbButton.setOnClickListener(listener); exitbButton.setOnClickListener(listener); Log.i(Tag, "onCreate"); } private OnClickListener listener=new OnClickListener() { @Override public void onClick(View v) { Button button=(Button)v; switch (button.getId()) { case R.id.dialog: /*new AlertDialog.Builder(MainActivity.this).setTitle("提示").setMessage("这是对话框?") .setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).show();*/ Intent intent=new Intent(MainActivity.this, SecondActivity.class); MainActivity.this.startActivity(intent); break; case R.id.exit: android.os.Process.killProcess(android.os.Process.myPid()); break; default: break; } } }; @Override protected void onStart() { Log.i(Tag, "onStart"); super.onStart(); } @Override protected void onRestart() { Log.i(Tag, "onRestart"); super.onRestart(); } @Override protected void onResume() { Log.i(Tag, "onResume"); super.onResume(); } @Override protected void onPause() { Log.i(Tag, "onPause"); super.onPause(); } @Override protected void onStop() { Log.i(Tag, "onStop"); super.onStop(); } @Override protected void onDestroy() { super.onDestroy(); } }
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import com.luku.ActivityLife.R; public class SecondActivity extends Activity { Button backbButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); backbButton=(Button)findViewById(R.id.back); backbButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { SecondActivity.this.finish(); } }); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="MainActivity" /> <Button android:text="对话框按钮" android:id="@+id/dialog" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> <Button android:text="退出" android:id="@+id/exit" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> </LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="SecondActivity" /> <Button android:text="返回MainActivity" android:id="@+id/back" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.luku.ActivityLife" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" 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=".SecondActivity" android:label="@string/app_name"> </activity> </application> </manifest>
$(document).ready(function(){dp.SyntaxHighlighter.HighlightAll('code');});
原文链接:
http://blog.csdn.net/yf210yf/article/details/6861481
相关文章推荐
- Android学习笔记04:Activity及Activity生命周期
- android学习笔记2:Activity的生命周期
- Android学习笔记之----Activity(一)生命周期
- android学习笔记09(activity的生命周期)
- Android学习笔记----Activity的生命周期图示
- (Android学习笔记)Activity的生命周期
- Android学习笔记(4):Activity生命周期
- android学习笔记之Activity生命周期详解
- Android学习笔记(08)——Android应用程序和Activity的生命周期
- Android学习笔记_18_Activity生命周期 及 跳转方式
- Android学习笔记-Activity生命周期(转自Hello Android)
- Android学习笔记(3)-Activity的生命周期
- Android学习笔记(3)-Activity的生命周期
- Android学习笔记之Activity的生命周期和启动模式
- Android学习笔记04:Activity及Activity生命周期
- android学习笔记之Activity生命周期
- Android学习笔记--Activity的生命周期
- Android学习笔记3 Activity的生命周期
- Android学习笔记(1)--关于activity的生命周期和启动模式
- Android学习笔记-Activity的生命周期