How to implement Up Navigation in Android for 2 parents who point to 1 child activity

I would like to know if it is possible to implement a navigation system in which one child activity can have two parent actions. Basically, I have a stream of content that the user can love. They can share the saved item via email, both from the activity of the stream, and from the activity that displays the "downloaded" content. I want to avoid class duplication simply because of navigation.

+6
source share
1 answer

Yes it is possible. But in the case of 2 or more parents, you cannot rely on the implementation of Up Navigation, as described here: Providing navigation up

So, you have 2 options left:

1- Use the behavior of the back button

You can do this simply by calling finish() or onBackPressed() in your onOptionsItemSelected(MenuItem item) android.R.id.home . Like this:

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; } 

2- Return to the first activity of your application

Return the user to the first activity your application starts with, for example:

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Intent backIntent = new Intent(this, YOUR_FIRST_ACTIVITY.class); backIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(backIntent); return true; } 

By the way, this question is a possible duplicate of this question

+10
source

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


All Articles