困惑的Android Activity singleInstance launchMode

时间:2015-03-30 13:00:04

标签: android android-activity

假设我有两个活动A(MAIN)和B,A中有一个按钮可以启动B,B中有一个按钮可以启动A.

活动A:

public class A extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            startActivity(new Intent(this, B.class));
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }   
}

活动B:

public class B extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            startActivity(new Intent(this, A.class));
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

AndroidManifest:

<application>
    <activity
        android:name=".A">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".B">
    </activity>
</application>

(1)当我在AndroidManifest中将A声明为singleInstance并将B保留为默认值时,运行应用程序,按此顺序启动活动:ABABABAB ....多次后,任务看起来像这样:

Task 1 : A
Task 2 : B

(2)当我在AndroidManifest中声明B为singleInstance而在默认情况下将A声明为A时,执行与(1)相同的操作,并得到此结果:

Task 1 : A
Task 2 : BBBBBBBBBBBB...

问题:为什么singleInstance在这两个场景中的工作方式不同?

0 个答案:

没有答案