android片段句柄后退按钮按下不起作用

时间:2014-08-25 08:15:36

标签: android fragment onkeydown

我正在处理片段,我用片段B.it的工作完美取代FragmentA,但是当我按下后退按钮时我无法返回片段A. 我试图覆盖onKeyDown方法,但我无法在片段中覆盖此方法 这是我的来源

 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();

当我使用CategoryViewPager并单击后退按钮时我无法返回(片段,我替换了这个片段) 我该如何解决我的问题?

3 个答案:

答案 0 :(得分:1)

在提交事务之前调用addToBackStack():

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

在您的情况下无需使用标签。就像这样使用

/*
     * 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);

答案 1 :(得分:0)

Check this link

检查addToBackStack(String name)方法。这应该可以解决你的问题。

答案 2 :(得分:0)

最好处理后退按钮按下你的活动,除非你的活动主持大量的碎片。当你按下碎片里面的按钮后,onBackPressed()你的活动方法将被调用,可以被覆盖和处理。所以处理碎片的后退按钮可以这种方式完成..

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();
            }

            }

片段1

 Fragment currentFragment = new Fragment2();

                MainActivity.isFragment1Shown=false;

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