I encountered an interesting issue, where an Activity is created multiple times, even it is defined as a singleTask or a singelInstance Activity in the manifest. Here is how this can be reproduced. Say, in the main activity:
@Override
protected void onResume() {
Intent i = new Intent(MainActivity.class, SingleActivity.class);
startActivity(i);
}
in my SingleActivity, I have:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
Log.i("SingleActivity", "onCreate " + System.identityHashCode(this));
...
}
and in the manifest, I have:
<activity android:name=".SingleActivity"
android:launchMode="singleInstance"
/>
now, if I start the application, things seem OK, expect in one case: if I press the 'back' button while SingleActivity
is in front, it navigates back to MainActivity
, where MainActivity.onResume()
will create another SingleActivity
instance, instead of bringing forward the one that already exists. this is something I know because on the log, a different identity hash code is displayed.
the same seems to be true if the launch mode is singleTask.
the only workaround seems to be to override onBackPressed()
, but that seems like an ugly solution.
I wonder what I'm doing wrong