1

I keep reading about fragment and fragment back handling but still couldn't find a best solution here.

I have navigation drawer.

Lets pretend I have 2 items in my navigation drawer list (item1,item2) when I click on item1 it's open A Fragment. in A Fragment after clicking in button opens B Fragment. So when B fragment is opened and I choose item2 from navigation drawer opens C Fragment.

And now when I press back button it should finish activity not go to B Fragment.

So in one word when I choose item1,item2 opens A fragment or C fragment, after pressing back button should finish activity but when opens B fragment(which is under item1) it have to go back to A Fragment

Please help it is very difficult for me. Thanks

public class MainActivity extends DefaultActivity {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggleWrapper mDrawerToggle;
    private ListView mDrawerListView;
    private TextView mTitle;
    private Runnable mPendingRunnable;
    private Handler mHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mDrawerListView = (ListView) findViewById(R.id.left_drawer);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mFragmentManager = getSupportFragmentManager();
        mHandler = new Handler();

        LayoutInflater inflator = (LayoutInflater) this .getSystemService(LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.custom_view, null);
        mTitle = (TextView)v.findViewById(R.id.title);

        getSupportActionBar().setBackgroundDrawable(null);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setDisplayUseLogoEnabled(false);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setIcon(R.drawable.button_indicator_menu);
        getSupportActionBar().setCustomView(v);

        v.findViewById(R.id.menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!mDrawerLayout.isDrawerOpen(GravityCompat.START)){
                    mDrawerLayout.openDrawer(GravityCompat.START);
                }else {
                    mDrawerLayout.closeDrawers();
                }
            }
        });

        mDrawerToggle = new ActionBarDrawerToggleWrapper(this, mDrawerLayout, R.drawable.transparent_gic, R.string.drawer_open, R.string.drawer_close){
            public void onDrawerClosed(View view) {
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                if (mPendingRunnable != null) {
                    mHandler.post(mPendingRunnable);
                    mPendingRunnable = null;
                }
            }

            public void onDrawerOpened(View drawerView) {
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerToggle.addPartnerToggle(new ContentDisplaceDrawerToggle(this, mDrawerLayout, R.id.content_frame));
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        setLeftDrawerLeftMenu();

        getFragment(getString(R.string.home));
        setTitle(getString(R.string.home));
    }

    private void setLeftDrawerLeftMenu(){
        final ArrayList<Item> items = new ArrayList<Item>();
        items.add(new SectionItem(this,"icons_small_home",getString(R.string.home)));

        items.add(new SectionItem(this,"icons_small_myacounts",getString(R.string.my_account)));
        items.add(new EntryItem(getString(R.string.bonuses)));
        items.add(new EntryItem(getString(R.string.account_recharge)));
        items.add(new EntryItem(getString(R.string.tariff_plans)));
        items.add(new EntryItem(getString(R.string.languages)));


        EntryAdapter adapter = new EntryAdapter(MainActivity.this, items);

        mDrawerListView.setAdapter(adapter);

        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                EntryItem item = (EntryItem) items.get(i);
                    setTitle(item.getTitle());
            }
        });
    }


    public void setTitle(final String title){
        mTitle.setText(title);
        mPendingRunnable = new Runnable() {
            @Override
            public void run() {
                getFragment(title);
            }
        };

        mDrawerLayout.closeDrawers();
    }

    public void getFragment(String name){
        if(getString(R.string.orange_shops).equalsIgnoreCase(name)){
            if (!(mSelectedFragment instanceof OrangeShopsFragment)){
                addPage(new MyFragment(MainActivity.this),true);
            }
        }else if(getString(R.string.home).equals(name)){
            addPage(new HomeFragment(MainActivity.this),false);
        }else{
            throw new RuntimeException("unknown fragment "+ name);
        }
    }

 public void addPage(final DefaultFragment pDefaultFragment, final boolean isAddToBackStack){
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        transaction.replace(R.id.content_frame, pDefaultFragment);
        if (isAddToBackStack) transaction.addToBackStack(null);
        transaction.commitAllowingStateLoss();
    }
}
bauer9664
  • 117
  • 1
  • 2
  • 8
  • for simple answere without using back stack [enter link description here](http://stackoverflow.com/a/34237594/5299555) – nakul Dec 12 '15 at 07:56
  • for simple answere without using back stack [You can find the solutions here](http://stackoverflow.com/a/34237594/5299555) – nakul Dec 12 '15 at 08:03

2 Answers2

3

It's not so difficult to clear the backstack.

Whenever the user clicks on any item on the drawer, just call

while(getSupportFragmentManager.popBackStackImmediate()){}

to clear what you had before.

edit:

I know it's a old question but I found a better/more efficient way of doing it using:

fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

that was answered in the groups.google.com by one of the Android engineers, I replaced it on my app and the "clear stack" command works much faster now. Hope it helps.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • I got this problem too, by adding this line it didn't work too, it did not clear backstack – fish40 Sep 19 '13 at 12:40
  • stuck in same issue http://stackoverflow.com/questions/34652142/stack-management-issue-with-navigation-drawer – albert Jan 07 '16 at 13:26
1

I also faced this problem while working with fragments and navigation drawer. When we are navigating from one fragment to another fragment they all are added to same backstack. So that's why you are getting this problem. Inorder to overcome this problem i myself maintained back stack and handled the on backpressed method

If you post your code i will suggest a nice solution for your problem

NARESH REDDY
  • 682
  • 4
  • 11