Question

Ok, so normally I'm not the asking kinda guy, but I couldn't solve my problem googling. So my first StackOverflow-Question.

I have an activity with a navigation drawer which triggers a few fragments, those fragments have subfragments of their own.

When the user goes deeper in the app, the 'hamburger'-icon is replaced by the up-caret. On pressing the caret or the back-key the subfragment is popped from the backstack and the hamburger-icon is back.

This is what happens in the subfragment:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // Called when the up caret in actionbar is pressed
        getActivity().onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

In the activity:

@Override
public void onBackPressed() {
    // turn on the Navigation Drawer image
    FragmentManager fragmentManager = getSupportFragmentManager();

    if (fragmentManager.getBackStackEntryCount () != 0)
        mDrawerToggle.setDrawerIndicatorEnabled(true);
    }
    super.onBackPressed();
}   

So far, so good.

The problem starts when we go deeper in the subfragment. The subfragments has a list and onLongClickItem it shows a contextual actionbar which lets users alter those listitems.

If that contextual actionbar is opened and the back-button is pressed, it closes (as expected) but also changes the up-caret to the drawer-hamburger-icon-thingie. Of course I do understand why this happens, but I don't see (or was able to find online) a clean solution.

Am I going at it the wrong way, or am I just missing a simple step here? I was thinking something along the lines of moving the setDrawerIndicatorEnabled to an onBackStackChangedListener and there listening for a tagged backStackItem.

But I thought; let's take some babysteps in to the wild and ask a question online before I start messing up all my code.

Thanks

Was it helpful?

Solution

The text below first was an edit on the question itselve, but because nobody seemed reluctant to answer my question I've added it as an answer so people who have a similar question are maybe helped by this.

If someone has a better answer, I'm still interested in a cleaner solution.


Ok, I managed to solve my own question, but I'm not realy sure I'm satisfied with it. What I did was add a fake entry to the backstack in onCreateActionMode, like so:

@Override
public boolean onCreateActionMode(ActionMode actionmode, Menu menu) {
    MenuInflater inflater = actionmode.getMenuInflater();
    inflater.inflate(R.menu.task_contextual_menu, menu);

    getActivity().getSupportFragmentManager()
                .beginTransaction()
                .addToBackStack(null)
                .add(new Fragment(), FAKE_BACKSTACK_ENTRY)
                .commit();

    return true;
}

and popping that back off in onDestroyActionMode and instead of checking in onBackPressed for (fragmentManager.getBackStackEntryCount () != 0), I check for (fragmentManager.getBackStackEntryCount () == 1).

I'm kind of proud of the workaround, but I realise it's not the cleanest of solutions...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top