尝试移动到上一个片段时,应用程序在按下后退按钮时关闭

时间:2014-10-31 13:02:50

标签: android android-fragments back-button

当我尝试从一个片段移动到前一个片段(它不会崩溃)时,我的应用程序关闭。这里是LandingActivity.java是我调用片段ChannelGrid.java的主要活动,它调用片段GridMain.java。当我按下片段GridMain应用程序中的移动按钮关闭而不是移动到ChannelGrid.java.I已经添加addToBackStack(“tag”)到片段并尝试使用onKey()..我在不同设备上测试了我的应用程序..

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

3 个答案:

答案 0 :(得分:1)

protected boolean onBackPressed(){

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

}

答案 1 :(得分:0)

在ChannelGrid.java中,在您将GridMain添加到FragmentManager的代码部分中,请务必为GridMain片段设置一个唯一标记(例如' GridMain'而不是' tag& #39)。通过这种方式,您将能够在以后与此片段进行通信:

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

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

在GridMain.java中添加一个函数来处理来自LandingActivity.java的后退按钮事件调用:

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

修改 同时更改'添加'替换'在ChannelGrid.java中,否则它将破坏R.id.content_frame

中的前一个片段

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