Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓在两个活动之间切换,无需重新创建和完成_Android_Android Intent - Fatal编程技术网

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

Android 安卓在两个活动之间切换,无需重新创建和完成,android,android-intent,Android,Android Intent,我有两个活动“A”和“B”。 我需要在没有完成和重新创建的情况下在它们之间切换 运行app->create and show A->press button->create and show B->press button->show existed A->press button->show existed B->依此类推 当前解决方案: private void toA() { Intent intentToA = new Intent(this, A.class); int

我有两个活动“A”和“B”。 我需要在没有完成和重新创建的情况下在它们之间切换

运行app->create and show A->press button->create and show B->press button->show existed A->press button->show existed 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 app -> 
A: onCreate, onResume -> 
press button -> 
B: onCreate, onResume -> 
press button -> 
A: onResume - > 
press button -> 
B: onResume -> 
...
Run app -> 
A: onCreate, onResume -> 
press button -> 
B: onCreate, onResume & A: onDestroy ->
press buttom ->
A: onCreate, onResume & B: onDestory ->
...
它在虚拟机上工作(原生,Genymotion-Android4.1)。活动将只创建一次。当我在它们之间切换时,一切都正常-不调用onCreate或onDestroy。这正是我想要的

但当我在真实设备(Nexus4-Android4.3)上运行相同的应用程序时,活动会在切换时被破坏和重新创建

在虚拟机上:

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 app -> 
A: onCreate, onResume -> 
press button -> 
B: onCreate, onResume -> 
press button -> 
A: onResume - > 
press button -> 
B: onResume -> 
...
Run app -> 
A: onCreate, onResume -> 
press button -> 
B: onCreate, onResume & A: onDestroy ->
press buttom ->
A: onCreate, onResume & B: onDestory ->
...
在真实设备上:

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 app -> 
A: onCreate, onResume -> 
press button -> 
B: onCreate, onResume -> 
press button -> 
A: onResume - > 
press button -> 
B: onResume -> 
...
Run app -> 
A: onCreate, onResume -> 
press button -> 
B: onCreate, onResume & A: onDestroy ->
press buttom ->
A: onCreate, onResume & B: onDestory ->
...

注意:Intent标志对切换行为没有影响。

AndroidManifest.xml
中将活动的
launchMode
设置为
singleInstance


希望这有帮助。

好的。我得到了它。我在2个活动中将launchMode设置为singleInstance。但我还做了一件事:在我的设备上,dev设置中选中了“不要保存操作。在操作完成后删除操作摘要”。谢谢。