15_activity生命周期方法说明
現在是可見并且可以被操作,所以現在是一個前臺的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 {@Overrideprotected 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));//顯式意圖 }@Overrideprotected void onStart() {// TODO Auto-generated method stubsuper.onStart();System.out.println("onStart");}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();System.out.println("onResume");}@Overrideprotected void onPause() {// TODO Auto-generated method stubsuper.onPause();System.out.println("onPause");}@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();System.out.println("onStop");}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();System.out.println("onDestroy");}@Overrideprotected void onRestart() {// TODO Auto-generated method stubsuper.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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {//onCreate()每一天都在跟它打交道super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);System.out.println("onCreate");}@Overrideprotected void onStart() {// TODO Auto-generated method stubsuper.onStart();System.out.println("onStart");}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();System.out.println("onResume");}@Overrideprotected void onPause() {// TODO Auto-generated method stubsuper.onPause();System.out.println("onPause");}@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();System.out.println("onStop");}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();System.out.println("onDestroy");}@Overrideprotected void onRestart() {// TODO Auto-generated method stubsuper.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" ><Buttonandroid: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" ><Buttonandroid: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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic 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-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" ><activityandroid: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" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world" /></RelativeLayout>
?
轉載于:https://www.cnblogs.com/ZHONGZHENHUA/p/7138905.html
總結
以上是生活随笔為你收集整理的15_activity生命周期方法说明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多校第八场
- 下一篇: python3安装scrapy问题解决