2

Hi I have created fragments in app drawer and now when I am pressing the back button on a fragment, it is closing the application.

There is already one existing onKeyDown in the webView of Main activity.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (myWebView.canGoBack()) {
                    myWebView.goBack();
                } else {
                    finish();
                }
                return true;
        }
    }
    return super.onKeyDown(keyCode, event);

For the onBackPressed method

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }

Fragment Impementation method,

if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_main, fragment);
        ft.addToBackStack("close");
        ft.commit();




    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
Shakti Patra
  • 75
  • 2
  • 12
  • 2
    Show us your implementation of fragments. – sharp Feb 23 '17 at 13:02
  • Can you tell how exactly you are adding your `fragments` when you click on your drawer item? – himanshu1496 Feb 23 '17 at 13:08
  • if (fragment != null) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.content_main, fragment); ft.commit(); } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); – Shakti Patra Feb 23 '17 at 13:28

4 Answers4

3

When you add a fragment to some container or inflate it using xml, it is not added to the backstack and so, when 'onBackPressed' occurs it throws the last activity from the backstack- which is the activity hosting the fragment and thus exiting the application. I think this post: Android: Fragments backStack

can help you, it explains how to add the fragment transaction to the backstack:

FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(..............);
    fragmentTransaction.addToBackStack("fragment transaction name, not required");
    fragmentTransaction.commit(); 
Community
  • 1
  • 1
Matan Koby
  • 232
  • 1
  • 11
  • Hi thanks for the solution but this didn't work for me.. I have reposted the question with my fragment implementation... – Shakti Patra Feb 23 '17 at 13:52
1

You need to do this in following way

@Override
public void onBackPressed() {
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
            //that means your stack is empty and you want to close activity
            finish();
        } else {
            // pop the backstack here
            getSupportFragmentManager().popBackStackImmediate();
        }
    }

}
yashkal
  • 713
  • 5
  • 20
0

I think you dont add the fragments to the backstack.

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
//your code
fragmentTransaction.addToBackStack("your_tag");
Tiko
  • 851
  • 8
  • 15
0

When you evoke a fragment add it to stack like this

fragmentTransaction.addToBackStack("fragment");

and then in onBackPress pop it from stack like this

FragmentManager fm = getActivity()
                .getSupportFragmentManager();
        fm.popBackStack ("fragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
Umesh Chauhan
  • 1,460
  • 1
  • 17
  • 26