FLAG_ACTIVITY_REORDER_TO_TOP raises a RuntimeException or black screen during rotation

I have two actions, and I want to switch between them without re-creating or duplicating them. Each action has a button that will send the user to another, using the intent with FLAG_ACTIVITY_REORDER_TO_TOP.

This works fine except for the following condition:

  • Launch the application after closing the power line.
  • press the button to go to activity 2
  • press the button to return to activity 1
  • rotate the screen.

At this point, the application crashes with "Performing an activity stop that does not resume." Lollipop just shows a black screen.

Oddly enough, if you go to the main screen before step 4 and restart the application, and then turn the above condition, you will not have any problems, and the application will work fine until it is closed / restarted.

This condition is similar to FLAG_ACTIVITY_REORDER_TO_TOP and occurs on Android 5.0 and 4.4 and only on a new start.

I have no attributes in the actions in the manifest. There is a button in the layouts that calls the method, which looks like this:

MainActivity: public void goSecond(View v) { Intent i = new Intent(this, SecondActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(i); } SecondActivity: public void goFirst(View v) { Intent i = new Intent(this, MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(i); } 

I do not redefine other life cycle methods and do nothing.

Any ideas?

+6
source share
1 answer

An interesting case, if you are debugging life cycle methods, you can see that onStop is called when the screen is onStop , without onPause . Then the failure is recorded

 java.lang.RuntimeException: Performing pause of activity that is not resumed: MainActivity 

followed by

 java.lang.RuntimeException: Performing stop of activity that is not resumed: MainActivity 

I assume this is a structure error. Interestingly, this only happens with the flag FLAG_ACTIVITY_REORDER_TO_FRONT .

From the moment of writing, I just found this post that clearly shows that there seems to be a major flaw in this flag: Is there an alternative to Intent.FLAG_ACTIVITY_REORDER_TO_FRONT (notification mentioned by Android Bug Ticket)


Workaround: If you depend on the behavior FLAG_ACTIVITY_REORDER_TO_FRONT gives you and you want to simulate it (sort), you can use

 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

instead (however, it works slightly differently - it clears the stack on top of the called activity, rather than reordering it, so you will have at least 1 re-creation of the activity).

Another option is to work around the launchMode attribute in the activity definition in the manifest. launchMode="singleInstance" may be what you need (however, this is useless with your task stack, so be careful)

After all, if your design allows this, consider snippets to avoid these problems altogether.

0
source

Source: https://habr.com/ru/post/982694/


All Articles