简体   繁体   中英

App closes on pressing back button when trying to move to previous fragment

My app closes when i try to move from one fragment to previous fragment(it doesn't crashes).Here LandingActivity.java is main activity in which I am calling fragment ChannelGrid.java which calls fragment GridMain.java.When i presses back button of mobile in fragment GridMain app closes rather than moving to ChannelGrid.java.I have added addToBackStack("tag") to fragments and also tried using onKey()..I tested my app on different devices also..

logcat verbose

   10-31 21:46:57.954  24452-24452/D/ActivityThread﹕ ACT-AM_ON_PAUSE_CALLED ActivityRecord{41eb8b98 token=android.os.BinderProxy@41bb9828 {xyz/xyz..activity.LandingActivity_}}
10-31 21:46:57.971  24452-24452/ D/ActivityThread﹕ ACT-PAUSE_ACTIVITY_FINISHING handled : 0 / android.os.BinderProxy@41bb9828
10-31 21:46:58.007  24452-24452/ V/InputMethodManager﹕ focusOut: android.widget.GridView@41f06f40 mServedView=android.widget.GridView@41f06f40 winFocus=false
10-31 21:46:58.297  24452-24452/ I/SurfaceTextureClient﹕ [0x5143bc58] frames:44, duration:1.002000, fps:43.883736
10-31 21:46:58.350  24452-24452/ D/OpenGLRenderer﹕ Flushing caches (mode 0)
10-31 21:46:58.432  24452-24452/ D/OpenGLRenderer﹕ Flushing caches (mode 0)
10-31 21:46:58.753  24452-24452/ D/OpenGLRenderer﹕ Flushing caches (mode 0)
10-31 21:46:58.754  24452-24452/ D/OpenGLRenderer﹕ Flushing caches (mode 0)
10-31 21:46:58.755  24452-24452/ D/OpenGLRenderer﹕ Flushing caches (mode 2)
10-31 21:46:58.879  24452-24452/ D/ActivityThread﹕ ACT-DESTROY_ACTIVITY handled : 1 / android.os.BinderProxy@41bb9828

protected boolean onBackPressed() {

FragmentManager fragmentManager;
fragmentManager = getFragmentManager();
fragmentManager.pop   BackStack();
return true;

}

In ChannelGrid.java, in the code portion where you are adding GridMain to the FragmentManager, be sure to set a unique tag to the GridMain fragment (eg 'GridMain' in stead of 'tag'). This way you will be able to communicate with this fragment later on:

frgManager.beginTransaction().replace(R.id.content_frame, currentFragment).addToBackStack("GridMain").commit(); // replaced "tag" with "GridMain"

The back button press event can be captured in LandingActivity.java:

@Override
public void onBackPressed() {
    FragmentManager frgManager;
    frgManager = getFragmentManager();

    Fragment fragment = fragmentManager.findFragmentByTag("GridMain");
    if (fragment != null) {
        GridMain gridMain = (GridMain) fragment;
        if (!gridMain.onBackPressed()) {
            super.onBackPressed();
        }
    }
    else {
        super.onBackPressed();
    }
}

In GridMain.java add a function to process the back button press event call from LandingActivity.java:

protected boolean onBackPressed() {
    FragmentManager frgManager;
    frgManager = getFragmentManager();
    frgManager.popBackStack();
    return true;
}

Edit: Also change 'add' to 'replace' in ChannelGrid.java, otherwise it will destroy the previous fragment in R.id.content_frame

This solved my problem..When you press back button inside your fragment onBackPressed() method of your activity will be called if you have declared that..So handling back button for fragments within navigation drawer can be one in this way..

MainActvity

     public static boolean isMainActivityShown ;
     public static boolean isFragment1Shown=false ;
     public static boolean isFragment2Shown=false ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isMainActivityShown=true  //inside onCreate method put isMainActivityShown true
. 
. 
.
}
                 Fragment currentFragment = new Fragment1();
                  isMainActivityShown=false;   //when moving to fragment1 
                isFragment1Shown=true;
               frgManager = getFragmentManager();
               frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
                                        .commit();
     @Override
        public void onBackPressed() {

            if(isMainActivityShown)
            {
                finish();
            }
            else if(isFragment1Shown)
            {
               //write the code to handle back button when you are in Fragment1
            }
           else if(isFragment2Shown)
            {  //When you are in Fragment 2 pressing back button will move to fragment1
                Fragment currentFragment = new Fragment1();
                isFragment2Shown=false;
    isFragment1Shown=true;

                FragmentManager frgManager;
                frgManager = getFragmentManager();
                frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
                        .commit();
            }

            }

Fragment1

Fragment currentFragment = new Fragment2();

            MainActivity.isFragment1Shown=false;
MainActivity.isFragment2Shown=true;
                            frgManager = getFragmentManager();
                            frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
                                    .commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM