0

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:

class diagram

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:

task expected task actual

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?

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
  • Strange. Have you tried this on any devices running API 28 or higher? Also, how do you know your task looks like this? What are you using to determine what activities are in the task? – David Wasser Jul 08 '20 at 14:32
  • No, I haven't tried this on any real device. I see this on the emulator. I do not know if the tasks look like this. I *assume* they look like that because of the behavior I see. On what I had a look is the `Activity Stack View` window in Android Studio. It didn't help me though. I also found that the `NEW_TASK` flag had some work on it in API 28: https://developer.android.com/about/versions/pie/android-9.0-changes-all#fant-required but currently I see no relation to the issue I face. – Bruno Bieri Jul 09 '20 at 09:25

0 Answers0