sena16
sena16

Reputation: 63

How to Implement Up Navigation in Android for 2 parents that point to 1 child activity

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

Answers (1)

MiaN KhaLiD
MiaN KhaLiD

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

Related Questions