CLEAR_TOP + start a new activity. Hierarchy problem

I have a hierarchy consisting of 4 depth levels. My profile has a 2nd hierarchy level, which means that when you click on a profile, you always need to return the user to the 1st hierarchy level.

My problem is that the profile link is available at all levels; so, for example, if a user located at the 4th level clicks on the profile link, his hierarchy position should be changed to the 2nd level - NOT by the fifth.

I believe that there should be a substantial way to do this. So far I have come up with the combination CLEAR_TOP to the 1st level + if(extra){startActivity(Profile)} , but this way the activity of the 1st level will be recreated - right? and therefore I want you to offer something better.

Thanks guys:)

UPD: just a little clarification: there might not be a profile instance, so just clearing the top will not work in this case.

+6
source share
2 answers

To prevent Level 1 MainActivity from being recreated instead of the usual CLEAR_TOP , I added Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP . According to the documentation, MainActivity gets onNewIntent , called if it is already created.

Having done this, I can switch according to the Intent and start any activity of the 2nd level.

+1
source

When you create an intention to return to your MainActivity, you can set both flags for CLEAR_TOP and NEW_TASK . Thus, he must return the existing activity, if any, or create another.

 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)); 
0
source

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


All Articles