Android系统.在两个活动之间切换,无需重新创建和完成

Bin*_*kot 7 android android-intent

我有2个活动"A"和"B".我需要在它们之间切换而不需要完成和重新创建.

运行app - >创建并显示A - >按下按钮 - >创建并显示B - >按下按钮 - >显示已存在A - >按下按钮 - >显示已存在B - >依此类推.

当前解决方案

private void toA() {
    Intent intentToA = new Intent(this, A.class);
    intentToA.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(intentToA);
}

private void toB() {
    Intent intentToB = new Intent(this, B.class);
    intentToB.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(intentToB);
}
Run Code Online (Sandbox Code Playgroud)

它适用于VM(native,Genymotion - android 4.1).活动只会创建一次.当我在它们之间切换时,每一个都没关系 - 没有onCreate或onDestroy的调用.这正是我想要的.

但是当我在真实设备(Nexus 4 - android 4.3)上运行相同的应用程序时,活动会在切换时被破坏并重新创建.

在VM上:

Run app -> 
A: onCreate, onResume -> 
press button -> 
B: onCreate, onResume -> 
press button -> 
A: onResume - > 
press button -> 
B: onResume -> 
...
Run Code Online (Sandbox Code Playgroud)

在真实设备上:

Run app -> 
A: onCreate, onResume -> 
press button -> 
B: onCreate, onResume & A: onDestroy ->
press buttom ->
A: onCreate, onResume & B: onDestory ->
...
Run Code Online (Sandbox Code Playgroud)

注意: Intent标志对开关行为没有影响.

Mus*_*eed 6

设置launchMode活动,singleInstanceAndroidManifest.xml

希望这可以帮助.