Activity的四种加载模式(转载)
生活随笔
收集整理的這篇文章主要介紹了
Activity的四种加载模式(转载)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在多Activity開發中,有可能是自己應用之間的Activity跳轉,或者夾帶其他應用的可復用Activity。可能會希望跳轉到原來某個Activity實例,而不是產生大量重復的Activity。這需要為Activity配置特定的加載模式,而不是使用默認的加載模式。
加載模式分類及在哪里配置
?
- standard
- singleTop
- singleTask
- singleInstance
standard
import android.content.Intent;?
import android.os.Bundle;?
import android.view.View;?
import android.view.View.OnClickListener;?
import android.widget.Button;?
import android.widget.LinearLayout;?
import android.widget.TextView; public class ActA extends Activity {?
? ? /** Called when the activity is first created. */?
? ? @Override?
? ? public void onCreate(Bundle savedInstanceState) {?
? ?? ???super.onCreate(savedInstanceState);?
? ?? ???TextView textView = new TextView(this);?
? ?? ???textView.setText(this + "");?
? ?? ???Button button = new Button(this);?
? ?? ???button.setText("go actA");?
? ?? ???button.setOnClickListener(new OnClickListener() {?
? ?? ?? ?? ?@Override?
? ?? ?? ?? ?public void onClick(View v) {?
? ?? ?? ?? ?? ? Intent intent = new Intent();?
? ?? ?? ?? ?? ? intent.setClass(ActA.this, ActA.class);?
? ?? ?? ?? ?? ? startActivity(intent);?
? ?? ?? ?? ?}?
? ?? ???});?
? ?? ???LinearLayout layout = new LinearLayout(this);?
? ?? ???layout.setOrientation(LinearLayout.VERTICAL);?
? ?? ???layout.addView(textView);?
? ?? ???layout.addView(button);?
? ?? ???this.setContentView(layout);?
? ? }?
} 例子中都沒有用layout,免得看著羅嗦。可見是ActA –> ActA的例子。在界面中打印出對象的toString值可以根據hash code識別是否創建新ActA實例。 第一個界面: 點擊按鈕后: 可以多點幾次。發現每次都創建了該Activity的新實例。standard的加載模式就是這樣的,intent將發送給新的實例。 現在點Android設備的回退鍵,可以看到是按照剛才創建Activity實例的倒序依次出現,類似退棧的操作,而剛才操作跳轉按鈕的過程是壓棧的操作。如下圖:
singleTop
import android.content.Intent;?
import android.os.Bundle;?
import android.view.View;?
import android.view.View.OnClickListener;?
import android.widget.Button;?
import android.widget.LinearLayout;?
import android.widget.TextView; public class ActA extends Activity {?
? ? /** Called when the activity is first created. */?
? ? @Override?
? ? public void onCreate(Bundle savedInstanceState) {?
? ?? ???super.onCreate(savedInstanceState);?
? ?? ???TextView textView = new TextView(this);?
? ?? ???textView.setText(this + "");?
? ?? ???Button button = new Button(this);?
? ?? ???button.setText("go actB");?
? ?? ???button.setOnClickListener(new OnClickListener() {?
? ?? ?? ?? ?@Override?
? ?? ?? ?? ?public void onClick(View v) {?
? ?? ?? ?? ?? ? Intent intent = new Intent();?
? ?? ?? ?? ?? ? intent.setClass(ActA.this, ActB.class);?
? ?? ?? ?? ?? ? startActivity(intent);?
? ?? ?? ?? ?}?
? ?? ???});?
? ?? ???LinearLayout layout = new LinearLayout(this);?
? ?? ???layout.setOrientation(LinearLayout.VERTICAL);?
? ?? ???layout.addView(textView);?
? ?? ???layout.addView(button);?
? ?? ???this.setContentView(layout);?
? ? }?
} ActB類: package com.easymorse.activities; import android.app.Activity;?
import android.content.Intent;?
import android.os.Bundle;?
import android.view.View;?
import android.view.View.OnClickListener;?
import android.widget.Button;?
import android.widget.LinearLayout; public class ActB extends Activity {?
? ? @Override?
? ? protected void onCreate(Bundle savedInstanceState) {?
? ?? ???super.onCreate(savedInstanceState);?
? ?? ?? ?Button button=new Button(this);?
? ?? ?? ?? ?button.setText("go actA");?
? ?? ?? ?? ?button.setOnClickListener(new OnClickListener() {?
? ?? ?? ?? ?? ? @Override?
? ?? ?? ?? ?? ? public void onClick(View v) {?
? ?? ?? ?? ?? ?? ???Intent intent=new Intent();?
? ?? ?? ?? ?? ?? ???intent.setClass(ActB.this, ActA.class);?
? ?? ?? ?? ?? ?? ???startActivity(intent);?
? ?? ?? ?? ?? ? }?
? ?? ?? ?? ?});?
? ?? ?? ?? ?LinearLayout layout=new LinearLayout(this);?
? ?? ?? ?? ?layout.addView(button);?
? ?? ?? ?? ?this.setContentView(layout);?
? ? }?
} ActB類使用默認(standard)加載,ActA使用singleTop加載。結果類似下圖: 如果把ActA的加載模式改為standard,情況一樣。
singleTask
singleTask模式和后面的singleInstance模式都是只創建一個實例的。 當intent到來,需要創建singleTask模式Activity的時候,系統會檢查棧里面是否已經有該Activity的實例。如果有直接將intent發送給它。 把上面singleTop的實例中的ActA的launchMode改為singleTask,ActB的改為standard。那么會發現在ActA界面中按一次按鈕: 然后在ActB1界面中按按鈕,因為ActA是singleTask,會使用原來的ActA1實例。這時候棧內的情況:
singleInstance
textView2.setText("task id: "+this.getTaskId()); 會發現,無論切換Activity,taskId是相同的。 當然也可以在這個單一的Task棧中,放入別人的Activity,比如google地圖,這樣用戶看過地圖按回退鍵的時候,會退棧回到調用地圖的Activity。對用戶來說,并不覺得在操作多個應用。這就是Task的作用。 但是,有這樣的需求,多個Task共享一個Activity(singleTask是在一個task中共享一個Activity)。 現成的例子是google地圖。比如我有一個應用是導游方面的,其中調用的google地圖Activity。那么現在我比如按home鍵,然后到應用列表中打開google地圖,你會發現顯示的就是剛才的地圖,實際上是同一個Activity。 如果使用上面三種模式,是無法實現這個需求的。google地圖應用中有多個上下文Activity,比如路線查詢等的,導游應用也有一些上下文Activity。在各自應用中回退要回退到各自的上下文Activity中。 singleInstance模式解決了這個問題(繞了這么半天才說到正題)。讓這個模式下的Activity單獨在一個task棧中。這個棧只有一個Activity。導游應用和google地圖應用發送的intent都由這個Activity接收和展示。 這里又有兩個問題:
?
- 如果是這種情況,多個task棧也可以看作一個應用。比如導游應用啟動地圖Activity,實際上是在導游應用task棧之上singleInstance模式創建的(如果還沒有的話,如果有就是直接顯示它)一個新棧,當這個棧里面的唯一Activity,地圖Activity回退的時候,只是把這個棧移開了,這樣就看到導游應用剛才的Activity了;
- 多個應用(Task)共享一個Activity要求這些應用都沒有退出,比如剛才強調要用home鍵從導游應用切換到地圖應用。因為,如果退出導游應用,而這時也地圖應用并未運行的話,那個單獨的地圖Activity(task)也會退出了。
轉載于:https://www.cnblogs.com/akira90/archive/2012/10/31/2747740.html
總結
以上是生活随笔為你收集整理的Activity的四种加载模式(转载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络爬虫详解
- 下一篇: 信捷plc485通信上位机_常用通信接口