androidandroid-fragmentsandroid-actionbarandroid-menuandroid-actionbar-compat

Android Fragments & Back button problems


I'm new in android fragments development.
I have this problem when hitting the back button... Neither the action bar menu nor its title are updating, even if i use myActivity.invalidateOptionMenu() and getActionBarActivity.setTitle() in : onAttach, onDetach, onCreate, onCreateView and in onCreateOptionMenu i cleared the menu, but it was all in vain!
And the same problem comes again sometimes when i re-instanciate a fragment using the drawerLayout!
For example i have ListingFragment, EditingFragment, DetailsFragment and ImportFragment Here is the ListingFragment with the EditingFragment options
PS: when i get back to the editing fragment, the menu does not work, not any more!

check the screen capture here

The second problem i am encountering is that some times, the fragments get added to the same container, see the screen capture bellow! the "import" word there is what the ImportFragment contains (plus the actionbar title), the other views (plus the menu) were in the screen when the fragment DetailsFragment was open. Once i hit the import button from the fragment it shows the following screen !

check the screen capture here

Does anyone know how to rearrange things to get a nice working app?

here is the relevant code:

private void setActionBarTitle(String title) {
    ((ActionBarActivity) getActivity()).getSupportActionBar().setTitle(
            title);

}


public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.clear();
    return super.onCreateOptionsMenu(menu);
}

public void restoreMenu()
{
    restoreActionBar();
    this.invalidateOptionsMenu();
}

Solution

  • For anyone who might as well face the same problem, i have found the way (or a way)

    Menu Problem : I am adding fragments not "replacing them". Each time a fragment does not have any "option menu" it keeps the last menu added to the action bar. It's either you stop adding and start replacing fragments or you inflate a new "empty" menu.

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        setHasOptionsMenu(true);
        menu.clear();
        inflater.inflate(R.menu.main, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
    

    where main is :

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.mypackage.MainActivity" > </menu>
    

    Back button problem: add the following code to your activity:

        @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
            this.finish();      } else {
            getSupportFragmentManager().popBackStack();
            if(fragmentX.isResumed())
                getSupportActionBar().setTitle("the title");
        }
    }
    

    PS: you can use isResumed or isVisible. Anything related to the action bar title: Keep a reference to the fragments you added. You can either change the action bar title using the fragment itself, work around and change the title using the activity by

    if(fragmentX.isVisible()) getSupportActionBar.setTitle(getString(R.string.your_string_name));