Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android Activity各啟動模式的差異,androidactivity

Android Activity各啟動模式的差異,androidactivity

編輯:關於android開發

Android Activity各啟動模式的差異,androidactivity


Activity共有四種啟動模式:standard,singleTop,singleTask,singleInstance

 

為了方便描述和理解,布局文件、Manifest文件和各個java文件如下:

AndoirdManifest文件

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.activitylaunchmode" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:launchMode="standard" android:screenOrientation="portrait" 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:launchMode="singleTop" android:screenOrientation="portrait" android:name=".SecondActivity"> </activity> <activity android:launchMode="singleTask" android:screenOrientation="portrait" android:name=".ThirdActivity"> </activity> <activity android:launchMode="singleInstance" android:screenOrientation="portrait" android:name=".FourActivity"> </activity> </application> </manifest> View Code

  4個Activity分別對應一種啟動模式:

    MainActivity     ---->  standard (默認模式,寫不寫都可以)

    SecondActivity ---->  singleTop

    ThirdActivty     ---->  singleTask

    FourActivity     ---->  singleInstance

布局文件 activity_main.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" 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="com.example.activitylaunchmode.MainActivity" > <TextView android:id="@+id/mTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_below="@id/mTextView" android:id="@+id/buttonOne" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" /> <Button android:id="@+id/buttonTwo" android:text="Button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/buttonOne" android:layout_below="@id/mTextView" /> <Button android:id="@+id/buttonThree" android:text="Button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/buttonOne" /> <Button android:id="@+id/buttonFour" android:text="Button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/buttonTwo" android:layout_toRightOf="@id/buttonThree" /> </RelativeLayout> View Code

MainActivity.java

package com.example.activitylaunchmode; 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; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener{ private static final String TAG = "MainActivity"; TextView tv; Button ButtonOne; Button ButtonTwo; Button ButtonThree; Button ButtonFour; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.mTextView); tv.setText("MainActivity"); ButtonOne = (Button) findViewById(R.id.buttonOne); ButtonOne.setOnClickListener(this); ButtonTwo = (Button) findViewById(R.id.buttonTwo); ButtonTwo.setOnClickListener(this); ButtonThree = (Button) findViewById(R.id.buttonThree); ButtonThree.setOnClickListener(this); ButtonFour = (Button) findViewById(R.id.buttonFour); ButtonFour.setOnClickListener(this); } @Override protected void onNewIntent(Intent intent) { // TODO Auto-generated method stub super.onNewIntent(intent); Log.d(TAG, "onNewIntent"); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.buttonOne: Intent mainIntent = new Intent(this,MainActivity.class); startActivity(mainIntent); break; case R.id.buttonTwo: Intent secondIntent = new Intent(this,SecondActivity.class); startActivity(secondIntent); break; case R.id.buttonThree: Intent ThirdIntent = new Intent(this,ThirdActivity.class); startActivity(ThirdIntent); break; case R.id.buttonFour: Intent FourIntent = new Intent(this,FourActivity.class); startActivity(FourIntent); break; } } } View Code

SecondActivity.java

package com.example.activitylaunchmode; 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; import android.widget.TextView; public class SecondActivity extends Activity implements OnClickListener { private static final String TAG = "SecondActivity"; TextView tv; Button ButtonOne; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.mTextView); tv.setText("SecondActivity"); ButtonOne = (Button) findViewById(R.id.buttonOne); ButtonOne.setOnClickListener(this); } @Override protected void onNewIntent(Intent intent) { // TODO Auto-generated method stub super.onNewIntent(intent); Log.d(TAG, "onNewIntent"); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.buttonOne: Intent secondIntent = new Intent(this,SecondActivity.class); startActivity(secondIntent); break; } } } View Code

ThirdActivity.java

package com.example.activitylaunchmode; 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; import android.widget.TextView; public class ThirdActivity extends Activity implements OnClickListener { private static final String TAG = "ThirdActivity"; TextView tv; Button ButtonOne; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.mTextView); tv.setText("ThirdActivity"); ButtonOne = (Button) findViewById(R.id.buttonOne); ButtonOne.setOnClickListener(this); } @Override protected void onNewIntent(Intent intent) { // TODO Auto-generated method stub super.onNewIntent(intent); Log.d(TAG, "onNewIntent"); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.buttonOne: Intent mainIntent = new Intent(this,MainActivity.class); startActivity(mainIntent); break; default: break; } } } View Code

FourActivity.java

package com.example.activitylaunchmode; 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; import android.widget.TextView; public class FourActivity extends Activity implements OnClickListener { private static final String TAG = "FourActivity"; TextView tv; Button ButtonOne; Button ButtonTwo; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.mTextView); tv.setText("FourActivity"); ButtonOne = (Button) findViewById(R.id.buttonOne); ButtonOne.setOnClickListener(this); } @Override protected void onNewIntent(Intent intent) { // TODO Auto-generated method stub super.onNewIntent(intent); Log.d(TAG, "onNewIntent"); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.buttonOne: Intent MainIntent = new Intent(this,MainActivity.class); startActivity(MainIntent); break; case R.id.buttonFour: Intent FourIntent = new Intent(this,FourActivity.class); startActivity(FourIntent); default: break; } } } View Code

 各個啟動模式差異探究:

  standard:  默認模式,每啟動一個activity都會在Task中創建一個,back鍵會依次從棧中退出

    MainActivity的啟動模式是standard, 點擊Button1,會再啟動一個MainActivity.

    通過adb shell dumpsys activity命令, 我們能看到在Task中存在兩個MainActivity.

    Task id #48
      TaskRecord{2cfa2efd #48 A=com.example.activitylaunchmode U=0 sz=2}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.example.activitylaunchmode/.MainActivity }
        Hist #1: ActivityRecord{37783c3a u0 com.example.activitylaunchmode/.MainActivity t48}
          Intent { cmp=com.example.activitylaunchmode/.MainActivity }
          ProcessRecord{358547f2 29163:com.example.activitylaunchmode/u0a134}
        Hist #0: ActivityRecord{363c8000 u0 com.example.activitylaunchmode/.MainActivity t48}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.example.activitylaunchmode/.MainActivity bnds=[801,812][1047,1060] }
          ProcessRecord{358547f2 29163:com.example.activitylaunchmode/u0a134}

    Running activities (most recent first):
      TaskRecord{2cfa2efd #48 A=com.example.activitylaunchmode U=0 sz=2}
        Run #1: ActivityRecord{37783c3a u0 com.example.activitylaunchmode/.MainActivity t48}
        Run #0: ActivityRecord{363c8000 u0 com.example.activitylaunchmode/.MainActivity t48}

  singleTop:如果要啟動的Activity在棧頂,則不會重新創建

    SecondActivity的啟動模式是singleTop, 點擊MainActivity中的Button2,會創建一個SecondActivity,再點擊SecondActivity中的Button1,會重新啟動 SecondActivity.

    通過adb命令,我們可以看到,Task中只有MainActivity和SecondActivity兩個Activity,第二次點擊並沒有重新創建。從log中,我們可以看到,第二次點擊啟動SecondActivity,只是調用了前一個SecondActivity的onNewIntent方法。

    Task id #51
      TaskRecord{fb41e01 #51 A=com.example.activitylaunchmode U=0 sz=2}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.example.activitylaunchmode/.MainActivity }
        Hist #1: ActivityRecord{1ea62f6c u0 com.example.activitylaunchmode/.SecondActivity t51}
          Intent { cmp=com.example.activitylaunchmode/.SecondActivity }
          ProcessRecord{69b44a6 30061:com.example.activitylaunchmode/u0a134}
        Hist #0: ActivityRecord{27f7bb0a u0 com.example.activitylaunchmode/.MainActivity t51}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.example.activitylaunchmode/.MainActivity bnds=[801,812][1047,1060] }
          ProcessRecord{69b44a6 30061:com.example.activitylaunchmode/u0a134}

    Running activities (most recent first):
      TaskRecord{fb41e01 #51 A=com.example.activitylaunchmode U=0 sz=2}
        Run #1: ActivityRecord{1ea62f6c u0 com.example.activitylaunchmode/.SecondActivity t51}
        Run #0: ActivityRecord{27f7bb0a u0 com.example.activitylaunchmode/.MainActivity t51}

  singleTask: 任務棧中沒有這個Activity,則會在任務棧中創建一個實例,如果任務棧中已經存在,則會將任務棧中的此activity之上的activity全部出棧

    ThirdActivity的啟動模式是singleTask, 點擊MainActivity中的Button3,啟動ThirdActivity,再點擊ThirdActivity中的Button1,啟動MainActivity,此時的Activity的堆棧信息如下:

    Task id #52
      TaskRecord{147eed75 #52 A=com.example.activitylaunchmode U=0 sz=3}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.example.activitylaunchmode/.MainActivity }
        Hist #2: ActivityRecord{2bf26a69 u0 com.example.activitylaunchmode/.MainActivity t52}
          Intent { cmp=com.example.activitylaunchmode/.MainActivity }
          ProcessRecord{3e2f10a 30540:com.example.activitylaunchmode/u0a134}
        Hist #1: ActivityRecord{147e2631 u0 com.example.activitylaunchmode/.ThirdActivity t52}
          Intent { flg=0x10000000 cmp=com.example.activitylaunchmode/.ThirdActivity }
          ProcessRecord{3e2f10a 30540:com.example.activitylaunchmode/u0a134}
        Hist #0: ActivityRecord{4e87389 u0 com.example.activitylaunchmode/.MainActivity t52}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.example.activitylaunchmode/.MainActivity bnds=[801,812][1047,1060] }
          ProcessRecord{3e2f10a 30540:com.example.activitylaunchmode/u0a134}

    Running activities (most recent first):
      TaskRecord{147eed75 #52 A=com.example.activitylaunchmode U=0 sz=3}
        Run #2: ActivityRecord{2bf26a69 u0 com.example.activitylaunchmode/.MainActivity t52}
        Run #1: ActivityRecord{147e2631 u0 com.example.activitylaunchmode/.ThirdActivity t52}
        Run #0: ActivityRecord{4e87389 u0 com.example.activitylaunchmode/.MainActivity t52}

    這個也說明了standard啟動模式會重新創建一個Activity.

    然後再點擊MainActivity中的Button3,啟動ThirdActivity,通過adb命令看到的如下: 

    Task id #53
      TaskRecord{2553d3b2 #53 A=com.example.activitylaunchmode U=0 sz=2}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.example.activitylaunchmode/.MainActivity }
        Hist #1: ActivityRecord{33f3f7cd u0 com.example.activitylaunchmode/.ThirdActivity t53}
          Intent { flg=0x10000000 cmp=com.example.activitylaunchmode/.ThirdActivity }
          ProcessRecord{1cdd8f03 30924:com.example.activitylaunchmode/u0a134}
        Hist #0: ActivityRecord{e5f9ea5 u0 com.example.activitylaunchmode/.MainActivity t53}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.example.activitylaunchmode/.MainActivity bnds=[801,812][1047,1060] }
          ProcessRecord{1cdd8f03 30924:com.example.activitylaunchmode/u0a134}

    Running activities (most recent first):
      TaskRecord{2553d3b2 #53 A=com.example.activitylaunchmode U=0 sz=2}
        Run #1: ActivityRecord{33f3f7cd u0 com.example.activitylaunchmode/.ThirdActivity t53}
        Run #0: ActivityRecord{e5f9ea5 u0 com.example.activitylaunchmode/.MainActivity t53}

      能看到Task中現在只有兩個Activity,ThirdActivity並沒有重新創建,靠後的一個MainActivity也被彈出棧,從log也能看出調用了onNewIntent方法,

  singleInstance: 只有一個實例,運行於獨立的task,啟動此Activity的時候如果已經創建,則不會重新創建

    FourActivity的啟動模式是singleInstance,點擊MainActivity中的Button4,會啟動FourActivity

    通過adb shell dumpsys activity命令,我們能看到兩個Activity在不同的Task中,    

    Task id #55
      TaskRecord{11be8b0d #55 A=com.example.activitylaunchmode U=0 sz=1}
      Intent { flg=0x10000000 cmp=com.example.activitylaunchmode/.FourActivity }
        Hist #0: ActivityRecord{1cd2b9e5 u0 com.example.activitylaunchmode/.FourActivity t55}
          Intent { flg=0x10000000 cmp=com.example.activitylaunchmode/.FourActivity }
          ProcessRecord{358d5ac2 31514:com.example.activitylaunchmode/u0a134}
    Task id #54
      TaskRecord{12e2f0d3 #54 A=com.example.activitylaunchmode U=0 sz=1}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.example.activitylaunchmode/.MainActivity }
        Hist #0: ActivityRecord{374dcd03 u0 com.example.activitylaunchmode/.MainActivity t54}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.example.activitylaunchmode/.MainActivity bnds=[801,812][1047,1060] }
          ProcessRecord{358d5ac2 31514:com.example.activitylaunchmode/u0a134}

    Running activities (most recent first):
      TaskRecord{11be8b0d #55 A=com.example.activitylaunchmode U=0 sz=1}
        Run #1: ActivityRecord{1cd2b9e5 u0 com.example.activitylaunchmode/.FourActivity t55}
      TaskRecord{12e2f0d3 #54 A=com.example.activitylaunchmode U=0 sz=1}
        Run #0: ActivityRecord{374dcd03 u0 com.example.activitylaunchmode/.MainActivity t54}

   如果再點擊FourActivity中的Button4,也不會重新創建,從log可以看出,會調用onNewIntent方法,這裡就不在貼activity的信息。

 

  adb shell dumpsys activity activities  也可以看到Task中各個activity的 launchMode.

 

 

    

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved