Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the parent activity of an activity at runtime?

I have an arbitrary number of hierarchically nested views/activities. The action bar should show the Up navigation button to navigate to a higher level in any view. For this, the google documentation says I have to set the parent activity with a tag in the activity's xml definition. However, I'm creating my activities dynamically and a child element can be of the same activity as it's parent.

So how do I set the parent activity to the actual parent instance at runtime?

like image 303
user3056893 Avatar asked Dec 04 '13 15:12

user3056893


People also ask

How to set parent activity in Android manifest?

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 we call parent activity from fragment?

Simply call your parent activity using getActivity() method.

What is the up button in Android Studio?

Add Up Button for Low-level Activities All screens in your app that are not the main entrance to your app (activities that are not the "home" screen) should offer the user a way to navigate to the logical parent screen in the app's hierarchy by pressing the Up button in the action bar.

What is onSupportNavigateUp?

onSupportNavigateUp() This method is called whenever the user chooses to navigate Up within your application's activity hierarchy from the action bar. @Nullable ActionMode.


1 Answers

It sounds like you are confusing up and back navigation.

The up button should be deterministic. From a given screen, the up button should always bring the user to the same screen.

The back button should not always bring the user to the same screen. The purpose of the back button is to help the user go backwards through your app chronologically. It should bring the user to the previous screen.

If there is no clear hierarchy of screens (e.g. there are no parent/child screens), then you may not need to implement up navigation at all.

See: Navigation with Up and Back

One option for overriding the default up button behavior is to simply intercept up button clicks and handle it yourself. For example:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        // Launch the correct Activity here
        return true;
    }
    return super.onOptionsItemSelected(item);
}
like image 101
Bryan Herbst Avatar answered Sep 23 '22 03:09

Bryan Herbst