I observe an unwanted behavior starting Android Emulator API level 28 when I recreate an activity. On back press I'm getting on the home screen instead of the previous activity like below on API level 27 and below.
So the situation is as follows:
In the AndroidManifest.xml
the MainActivity
is set as singleTask
. Both the MainActivity
and the SettingsActivity
extend the BaseActivity
.
In the SettingsActivity
I let the user change to dark mode or another language and then I recreate the activity with:
getActivity().recreate();
The BaseActivity
then recreates itself (approach from here), if necessary according to the (new) preferences like:
startActivity(getIntent());
finish();
overridePendingTransition(0, 0);
The recreation works fine. On API level 27 and below I assume I get a task looking like the one on the left and from API 28 upwards I assume I get one looking like the one on the right:
But I would also expect a task looking like the first one for API 28 and upwards.
I figured out that in the BaseActivity
I could add the Intent.FLAG_ACTIVITY_CLEAR_TASK
Intent flag to recreate the activity if necessary which gives me the desired behavior but I don't understand why this actually helps and if this is the best thing to do here. Any Intent/Task expert here?
What do I miss here?