Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Handle onBack Press in Fragment Android

I have 3 Fragments in my application like HomePage , Frag1 ,Frag2,Frag3,Frag4.

So what is happening i have used navigation drawer in my application .On the home page i have a grid view that have the link of all the four fragments. If the user navigates from Homepage -> Frag1 -> Frag1.2 ->Frag1.3 ->HomePage.

So onBack press i want to show the previous fragments but once the user in on the Home Page and if he press the back i want to close my application.I am not getting the way how we can do this.

This is how i am calling the Fragments from the Navigation Drawer:

protected void getUrl(int position) {
        FragmentTransaction fragTran = getSupportFragmentManager()
                .beginTransaction();
        switch (position) {
        case 0:
            fragTran.replace(R.id.content_frame, dashboardFragment);
            fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            // fragTran.addToBackStack(null);
            fragTran.commit();
            break;
        case 1:

            if (CommonUtils.isNetworkConnected(getApplicationContext())) {
                fragTran.replace(R.id.content_frame, frag1);
                fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                fragTran.addToBackStack(null);
                fragTran.commit();

            } else {
                Toast.makeText(getApplicationContext(),
                        "No Network Connection", Toast.LENGTH_LONG).show();
            }
            break;
        case 2:

            if (CommonUtils.isNetworkConnected(getApplicationContext())) {
                fragTran.replace(R.id.content_frame, frag2);
                fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                fragTran.addToBackStack(null);
                fragTran.commit();

            } else {
                Toast.makeText(getApplicationContext(),
                        "No Network Connection", Toast.LENGTH_LONG).show();
            }
            break;
        case 3:
            if (CommonUtils.isNetworkConnected(getApplicationContext())) {
                fragTran.replace(R.id.content_frame, frag3);
                fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                fragTran.addToBackStack(null);
                fragTran.commit();

            } else {
                Toast.makeText(getApplicationContext(),
                        "No Network Connection", Toast.LENGTH_LONG).show();
            }

            break;



        default:
            // return "";
        }
    }

This is how i am calling from the GridView onClick :

final FragmentTransaction fragTran = getFragmentManager()
                .beginTransaction();

        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int arg2,
                    long arg3) {
                if (DASHBOARD_LINKS[arg2].equals("frag1")) {
                    if (CommonUtils.isNetworkConnected(getActivity())) {
                        fragTran.replace(R.id.content_frame,
                                frag1);
                        fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                        fragTran.addToBackStack(null);
                        fragTran.commit();
                    } else {
                        Toast.makeText(getActivity(), "No Network Connection",
                                Toast.LENGTH_LONG).show();
                    }
                }
                if (DASHBOARD_LINKS[arg2].equals("frag2")) {
                    fragTran.replace(R.id.content_frame, frag2);
                    fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    fragTran.addToBackStack(null);
                    fragTran.commit();
                }
                if (DASHBOARD_LINKS[arg2].equals("frag3")) {
                    fragTran.replace(R.id.content_frame, frag3);
                    fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    fragTran.addToBackStack(null);
                    fragTran.commit();
                }
            }
        });
like image 797
Pooja Dubey Avatar asked Feb 14 '23 13:02

Pooja Dubey


1 Answers

Add your fragments like this:

int count = fragmentManager.getBackStackEntryCount();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, dashboardFragment)
        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
        .addToBackStack(String.valueOf(count))
        .commit();

Then, when you click on your grid, just add them as shown above. But if you click on item in nav drawer, do this before:

fragmentManager.popBackStackImmediate("0", 0); // "0" here is a tag of transaction you added before. "0" is for first added fragment (your home fragment, I assume). You can pop back stack to second with "1", if you like

This will clear the memory of previous fragments and you will not go back to them on "back" pressed.

And finally, implement this in your activity:

@Override
public void onBackPressed() {
    if (fragmentManager.getBackStackEntryCount() <= 1) {
        finish();      
        return;
    }
    super.onBackPressed();
}

This will finish your app if you are in your first home fragment.

like image 57
Dmide Avatar answered Feb 16 '23 03:02

Dmide