Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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.

like image 789
sena16 Avatar asked Nov 10 '13 18:11

sena16


People also ask

How to add parent activity in android?

Declare a Parent Activity You can do this in the app manifest, by setting an android:parentActivityName attribute. The android:parentActivityName attribute was introduced in Android 4.1 (API level 16). To support devices with older versions of Android, define a <meta-data> name-value pair, where the name is "android.

How can I display one activity inside another android?

You can use a Fragment API to complete the task. See full details in developer's guide. Then create a MyFragment class and load it when appropriate.

What is android parentActivityName?

As per docs -> section android:parentActivityName : The system reads this attribute to determine which activity should be started when the user presses the Up button in the action bar. The system can also use this information to synthesize a back stack of activities with TaskStackBuilder .

How can we call parent activity from fragment?

Simply call your parent activity using getActivity() method.


1 Answers

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

like image 90
MiaN KhaLiD Avatar answered Nov 10 '22 18:11

MiaN KhaLiD