gpt4 book ai didi

android - 应用程序在抽屉导航内的 fragment 内按下后退按钮时退出

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:28:44 24 4
gpt4 key购买 nike

当我尝试从子 fragment 移动到父 fragment 时,我的应用程序退出(它不会崩溃)。这里 LandingActivity.java 是我调用 fragment ChannelGrid.java 的主要 Activity ,它调用 fragment GridMain.java。当我在 fragment 中按下移动设备的后退按钮 GridMain 应用程序退出而不是移至 ChannelGrid.java。我已将 addToBackStack("tag") 添加到 fragment 中,还尝试使用 onKey()..我也在不同的设备上测试了我的应用程序..我检查了找出具有相同问题的其他解决方案并进行了尝试,但没有任何效果..

抽屉导航-LandingActivity.java-ChaanelGrid.java(Fragment)-GridMain.java(Fragment)

登陆 Activity .java

   currentFragment = new ChaanelGrid();

currentFragment.setArguments(args);
frgManager = getFragmentManager();

frgManager.beginTransaction().add(R.id.content_frame, currentFragment).addToBackStack("tag")
.commit();

ChanelGrid.java

Fragment currentFragment = new GridMain();



FragmentManager frgManager;
frgManager = getFragmentManager();
frgManager.beginTransaction().add(R.id.content_frame, currentFragment).addToBackStack("GridMain")
.commit()

GridMain.java(我也尝试过不使用 onKey() 方法但没有成功)

rootView.setOnKeyListener(new View.OnKeyListener() {


@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
Fragment currentFragment = new ChaanelGrid();


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

详细的 logcat

   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

我尝试在 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 中

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

使用 Log 我检查了 LandingActivity.java 的 onBackPressed() 被调用但仍然是相同的输出..

最佳答案

这就是我解决它的方法..当你在你的 fragment 中按下后退按钮时,如果你声明了你的 Activity 的 onBackPressed() 方法将被调用..因此处理抽屉导航中 fragment 的后退按钮可以是其中之一方式..

主要 Activity

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

}

fragment 1

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

关于android - 应用程序在抽屉导航内的 fragment 内按下后退按钮时退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26687485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com