Reputation: 63
I'd like to find out if it is possible to implement a navigation system where one child activity can have two parent activities. Basically, I have a stream of content which a user may favourite. They can share a saved item via email, from both the stream activity and an activity which displays "favourited" content. I want to avoid duplicating a class simply because of navigation.
Upvotes: 6
Views: 2585
Reputation: 1518
Yes, it is possible. But in case of 2 or more parents you cant rely on the implementation of Up Navigation as described here: Providing Up Navigation
So, you are left with 2 options:
1- Use the back button behavior
You can do this by just calling finish()
or onBackPressed()
in your onOptionsItemSelected(MenuItem item)
's android.R.id.home
case. Like this:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
2- Go back to the first activity of your app
Take the user back to first activity from where your app starts, like this:
@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
Upvotes: 11