Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I control the activity's up button from a contained fragment?

I'm building a pretty simple app. At it's core are 2 screens:
1) list-screen: a list of items
2) detail-screen: a detailed view of an item

I used one Activity (which extends AppCompatActivity) with an Action-Bar, a Navigation-Drawer and a main-content part (a FrameLayout).
I used 2 different fragments for the 2 screens:

When opening the app I inflate the list-fragment into the main-content part.
When an item in the list is clicked I inflate the detail-fragment into the main-content part and it all works well. My App Layout
On the detail-screen I want the Action-Bar to display an up-button that goes back to the list-screen.
Considering the fact that I am using fragments, rather than separate activites, how can I achieve that?

like image 916
BeFree Avatar asked Dec 01 '15 16:12

BeFree


People also ask

How do I open a button with a fragment?

You can replace the fragment using FragmentTransaction on button click. Something like this: Fragment someFragment = new SomeFragment(); FragmentTransaction transaction = getFragmentManager(). beginTransaction(); transaction.

How do you communicate from fragment to activity?

To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the Interface methods to communicate with the Activity.

How do I access the activity toolbar in fragment?

Though you can add a Toolbar anywhere within your fragment's view hierarchy, you should generally keep it at the top of the screen. To use the Toolbar in your fragment, provide an ID and obtain a reference to it in your fragment, as you would with any other view.


1 Answers

You can enable the up button each time the Detail Fragment loads, and disable it whenever any of the other Fragments load.

First define these methods in your Activity, which you can call from the Fragments in order to show/hide the up button:

public void showUpButton() {
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

public void hideUpButton() {
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}

You will probably want to just enable the up button in on Resume() of the detail Fragment (MainActivity is just an example, change to the name of your Activity) .....

@Override
public void onResume() {
    super.onResume();
    MainActivity activity = (MainActivity)getActivity();
    if (activity != null) {
      activity.showUpButton();
    }

}

Then in the other Fragments:

@Override
public void onResume() {
    super.onResume();
    MainActivity activity = (MainActivity)getActivity();
    if (activity != null) {
      activity.hideUpButton();
    }

}

The next thing is to make the up button actually go back. First ensure that you're adding the Fragment with the up button to the back stack, and then add this to that Fragment.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        ((MainActivity)getActivity()).onBackPressed();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

Then in the Activity, override onBackPressed() and pop from the back stack if the FragmentManager has any entries:

@Override
public void onBackPressed() {

    FragmentManager fragmentManager = getSupportFragmentManager();
    if (fragmentManager.getBackStackEntryCount() > 1) {
        fragmentManager.popBackStackImmediate();
    } else {
        super.onBackPressed();
    }
}
like image 102
Daniel Nugent Avatar answered Sep 25 '22 17:09

Daniel Nugent