现在是可见并且可以被操作,所以现在是一个前台的Activity。
按一下Home键,它是先onPause然后onStop.
现在它就处于一个Stop停止的状态。停止的状态如果我当前内存够用的情况下,它会依然保留当前的所有信息。
如果按多一次Home键还可以把这个东西重新给调出来。
应该不是实体键设置的问题,是操作的问题。按住电脑的home键再点击安卓虚拟设备/模拟器AVD里面的实体键按钮home就可以把当前正在运行的进程调出来。记住电脑的小键盘num lock要打开才可以使用小键盘的home键。
只不过这个时候它是不可见不可被操作。但是所有的状态还会被保存。
重新再把进程调出来运行,这个时候它又处于可见并且可以被操作的状态。这些就是这些生命周期方法。
什么时候它会处于onPause?
为了能让它正确打开必须在清单文件里面声明第二个Activity。怎么能让它处于一个暂停的状态,创建一个透明的应用。
Theme.Translucent.NoTitleBar.Fullscreen
透明主题没有标题栏并且是全屏的。
先运行Day10_06_activity生命周期再运行Day10_07_透明应用,Day10_06_activity生命周期这个应用可见但是不可操作,它处于暂停状态onPause了.
如果开启了应用Day10_06_activity生命周期,然后按一下返回键就onPause->onStop->onDestroy.
如果是处于前台状态,那么就说明onResume被执行完了。前台状态->暂停状态,要执行onPause().暂停状态->前台状态,要执行onResume().
前台状态->停止状态,要执行onPause()和onStop().
销毁状态,就是onPasue()->onStop()->onDestroy().
从停止状态->前台状态,执行onrestart()->onstart()->onresume().但是没有执行onCreate(),说明Activity还是以前的,并没有创建出一个新的Activity.
Activity生命周期的七个方法必须得牢记在心。
package com.itheima.activitylifecycle; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) {//onCreate()每一天都在跟它打交道 super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); System.out.println("onCreate"); } public void open(View v){ startActivity(new Intent(this, SecondActivity.class));//显式意图 } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); System.out.println("onStart"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("onResume"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); System.out.println("onPause"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); System.out.println("onStop"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); System.out.println("onDestroy"); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); System.out.println("onRestart"); } }
package com.itheima.activitylifecycle; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) {//onCreate()每一天都在跟它打交道 super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); System.out.println("onCreate"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); System.out.println("onStart"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("onResume"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); System.out.println("onPause"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); System.out.println("onStop"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); System.out.println("onDestroy"); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); System.out.println("onRestart"); } }
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="open" android:text="打开另一个activity" /> </RelativeLayout>
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="open" android:textColor="#ff0000" android:text="打开另一个activity" /> </RelativeLayout>
package com.itheima.translucent; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.translucent" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" > <activity android:name="com.itheima.translucent.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> </application> </manifest>
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>