简体   繁体   中英

android fragment handle back button press not working

I am working fragment, I replaced FragmentA by fragment B.it's working perfect,but when I press back button I can't back Fragment A. I tried to override onKeyDown method,but I can't override this method in fragment this is a my source

 CategoryViewPager fragment1 = new CategoryViewPager();
 FragmentTransaction ft = getSupportFragmentManager()
                    .beginTransaction().addToBackStack("method");

 ft.setCustomAnimations(R.anim.trans_left_in, R.anim.trans_left_out);
 ft.replace(R.id.content_frame, fragment1, "fragment2");
 ft.commit();

when I use CategoryViewPager and click back button I can't go back(fragment wich i replaced this fragment) how can I solve my problem?

Call addToBackStack() before you commit the transaction:

    getSupportFragmentManager().beginTransaction()
                               // Add this transaction to the back stack
                               .addToBackStack()
                               .commit();

No need to use tag in your case. just use like this

/*
     * Add this transaction to the back stack. 
     * This means that the transaction will be remembered after it is 
     * committed, and will reverse its operation when later popped off 
     * the stack.
     */
    fragmentTransaction.addToBackStack(null);

Check this link

Check the addToBackStack(String name) method. This should solve your problem.

It is better to handle back button press through your activity unless your actvity host large number of fragments.When you press back button inside your fragment onBackPressed() method of your activity will be called which can be overridden and handled..So handling back button for fragments can be done 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