当我尝试从子片段移动到父片段(它没有崩溃)时,我的应用程序退出。这里是LandingActivity.java,我调用的片段是调用片段GridMain.java的片段ChannelGrid.java。当我在片段GridMain应用程序退出时按下移动按钮,而不是移动到ChannelGrid.java。我已经添加了addToBackStack(" tag")到片段并尝试使用onKey()..我测试了我的应用程序不同的设备也..我检查了相同问题的其他解决方案并尝试了但没有任何作用..
Navigation Drawer-LandingActivity.java-ChaanelGrid.java(Fragment)-GridMain.java(Fragment)
LandingActivity.java
currentFragment = new ChaanelGrid();
currentFragment.setArguments(args);
frgManager = getFragmentManager();
frgManager.beginTransaction().add(R.id.content_frame, currentFragment).addToBackStack("tag")
.commit();
ChaanelGrid.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 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
我尝试在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(),但仍然是相同的输出..
答案 0 :(得分:4)
这就是我解决它的方法..当你按下片段里面的后退按钮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();
答案 1 :(得分:3)
这就是我通常处理后退按钮的方式:
// "State Machine" variables: to indicate which is the active Fragment.
private static boolean isHelpShown = false;
protected static boolean isInfoShown = false;
protected static boolean isMainShown = false;
private static boolean isViewShown = false;
// Used to switch between Fragments.
private static enum Fragments
{
MAIN,
VIEW,
HELP,
INFO
}
@Override
public final void onBackPressed()
{
if (isMainShown)
{
// We're in the MAIN Fragment.
finish();
}
else
{
// We're somewhere else, reload the MAIN Fragment.
showFragment(Fragments.MAIN);
}
}
private final void showFragment(final Fragments FragmentType)
{
isViewShown = false;
isHelpShown = false;
isInfoShown = false;
isMainShown = false;
final FragmentTransaction ft =
getSupportFragmentManager().beginTransaction();
/*
Replace whatever is in the fragment_container view with this
fragment, and add the transaction to the back stack so the user can
navigate back.
*/
switch(FragmentType)
{
case HELP:
{
ft.replace(R.id.frgMaster, new FRG_Help());
isHelpShown = true;
break;
}
case INFO:
{
ft.replace(R.id.frgMaster, new FRG_Info());
isInfoShown = true;
break;
}
case VIEW:
{
ft.replace(R.id.frgMaster, new FRG_View());
isViewShown = true;
break;
}
case MAIN:
{
ft.replace(R.id.frgMaster, new FRG_Main());
isMainShown = true;
break;
}
}
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// Finalize the transaction...
ft.commit();
getSupportFragmentManager().executePendingTransactions();
}
正如你所看到的,根据我的需要,我只处理当我们处于主要片段(我退出)或当我们不在时(我回到MAIN片段:另一个Back press并退出)。 / p>
您可以在onBackPressed方法中添加更多检查,以检查您是否在另一个片段中并相应地加载另一个片段。
答案 2 :(得分:0)
我建议使用TAG替换片段来产生相同的结果。
简单示例如下所示
使用TAG
将片段B添加到活动中fragmentTransaction.replace(R.id.main_fragment_container,new FragmentB(),“TAG_B”);
片段A - >片段B [onBackPressed] - >片段A覆盖Activity文件中的onBackPressed(),其中,
//检查片段B,你正在查看片段B
if(getSupportFragmentManager()。findFragmentByTag(“TAG_B”)!= null) {fragmentTransaction.replace(R.id.main_fragment_container,new FragmentA(),“TAG_A”);
}
答案 3 :(得分:0)
我尝试了这里建议的所有解决方案,但没有一个对我有效。 这就是我最终通过修改最高投票答案来解决它的问题:
在托管片段的活动中,声明FragmentManager
private FragmentManager fragmentManager;
在onCreateMethod中初始化它:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager();
...
最后,在你的onBackPressed方法中执行以下操作:
public void onBackPressed() {
if(fragmentManager.getBackStackEntryCount() != 0) {
fragmentManager.popBackStack();
if (IS_MAIN_SHOWN) {
finish();
} else {
displayView(0); //this is the method that changes the fragments, takes an int argument for the position of the fragment.
}
} else {
super.onBackPressed();
}
}