我有4个片段,即A B C D. 现在当我从A到B更多时,我将A添加到后栈。 我没有添加到后台,而是从B移动到C. 我没有添加到后台,而是从C移动到D. 现在在背面按下我被重定向到A.
我再次重复直到D,现在当我按下时,它显示片段A并再次将我重定向到片段D,然后再次按下,它会退出应用程序。
我希望它像是从D背面按下它应该总是让我到A
任何人都可以帮忙吗?
这是代码 https://drive.google.com/folderview?id=0Bwt5u0ivqfvPSmxNN0V4YkNKa1U&usp=sharing
我在logcat中得到这个
02-06 13:31:19.395 26930-26930 / com.example.user.fragmentbackstackexample I / dalvikvm:找不到方法android.view.ViewGroup.isTransitionGroup,从方法引用 android.support.v4.app.FragmentTransitionCompat21.captureTransitioningViews
02-06 13:31:19.395 26930-26930 / com.example.user.fragmentbackstackexample W / dalvikvm:VFY:无法解析虚方法11340:Landroid / view / ViewGroup; .isTransitionGroup()Z
02-06 13:31:19.395 26930-26930 / com.example.user.fragmentbackstackexample I / dalvikvm:找不到方法android.view.View.getTransitionName,从方法android.support.v4.app.FragmentTransitionCompat21引用.findNamedViews
02-06 13:31:19.395 26930-26930 / com.example.user.fragmentbackstackexample W / dalvikvm:VFY:无法解析虚方法11182:Landroid / view / View; .getTransitionName()Ljava / lang / String ;
02-06 13:31:19.405 26930-26930 / com.example.user.fragmentbackstackexample I / dalvikvm:找不到方法android.view.View.getTransitionName,从方法android.support.v4.app.FragmentTransitionCompat21引用.getTransitionName
02-06 13:31:19.405 26930-26930 / com.example.user.fragmentbackstackexample W / dalvikvm:VFY:无法解析虚方法11182:Landroid / view / View; .getTransitionName()Ljava / lang / String ;
02-06 13:31:19.415 26930-26930 / com.example.user.fragmentbackstackexample W / dalvikvm:无法解析Landroid / support / v4 / app / FragmentTransitionCompat21 $ 1的超类; (1303)
02-06 13:31:19.415 26930-26930 / com.example.user.fragmentbackstackexample W / dalvikvm:类的链接' Landroid / support / v4 / app / FragmentTransitionCompat21 $ 1;'失败
02-06 13:31:19.415 26930-26930 / com.example.user.fragmentbackstackexample E / dalvikvm:找不到类' android.support.v4.app.FragmentTransitionCompat21 $ 1',引用从方法 android.support.v4.app.FragmentTransitionCompat21.setEpicenter
02-06 13:31:19.425 26930-26930 / com.example.user.fragmentbackstackexample W / dalvikvm:VFY:无法解析新实例294 (Landroid / support / v4 / app / FragmentTransitionCompat21 $ 1;)在Landroid / support / v4 / app / FragmentTransitionCompat21;
02-06 13:31:19.425 26930-26930 / com.example.user.fragmentbackstackexample W / dalvikvm:无法解析Landroid / support / v4 / app / FragmentTransitionCompat21 $ 3的超类; (1303)
02-06 13:31:19.425 26930-26930 / com.example.user.fragmentbackstackexample W / dalvikvm:类的链接' Landroid / support / v4 / app / FragmentTransitionCompat21 $ 3;'失败
02-06 13:31:19.435 26930-26930 / com.example.user.fragmentbackstackexample E / dalvikvm:找不到类' android.support.v4.app.FragmentTransitionCompat21 $ 3',引用来自方法android.support.v4.app.FragmentTransitionCompat21.setSharedElementEpicenter
02-06 13:31:19.435 26930-26930 / com.example.user.fragmentbackstackexample W / dalvikvm:VFY:无法在Landroid中解析新实例296(Landroid / support / v4 / app / FragmentTransitionCompat21 $ 3;)性/支撑性/ V4 /应用程序/ FragmentTransitionCompat21;
02-06 13:31:19.435 26930-26930 / com.example.user.fragmentbackstackexample W / dalvikvm:无法解析Landroid / support / v4 / app / FragmentTransitionCompat21 $ 1的超类; (1303)
02-06 13:31:19.435 26930-26930 / com.example.user.fragmentbackstackexample W / dalvikvm:类的链接' Landroid / support / v4 / app / FragmentTransitionCompat21 $ 1;'失败
02-06 13:31:19.445 26930-26930 / com.example.user.fragmentbackstackexample W / dalvikvm:无法解析Landroid / support / v4 / app / FragmentTransitionCompat21 $ 3的超类; (1303)
02-06 13:31:19.445 26930-26930 / com.example.user.fragmentbackstackexample W / dalvikvm:类的链接' Landroid / support / v4 / app / FragmentTransitionCompat21 $ 3;'失败
答案 0 :(得分:0)
这样做.. 对于片段A这样做..
getSupportFragmentManager().beginTransaction()
.add(R.id.containerMain, new FragmentA()).addToBackStack("BACKSTACK_FRAGMENT_A")
.commit();
对于其他片段,请不要使用 addToBackStack(..)
现在,当您从其他片段向后按时,您将始终返回片段A.
更新:
在您的MainActivity.java中执行此操作..
getSupportFragmentManager().beginTransaction().add(android.R.id.content,new FragmentA()).addToBackStack("BACKSTACK_FRAGMENT_A").commit();
在你的FragmentA.java中这样做..
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((Button) mView.findViewById(R.id.buttonA)).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
setFragment(new FragmentB());
}
});
}
最终更新:
实现愿望的最简单方法..
在 MainActivity.java 中执行此操作..
public String TAG = "";
@Override
public void onBackPressed() {
if (TAG.equals("FragmentD")) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new FragmentA()).commit();
}else{
super.onBackPressed();
}
}
在每个片段中执行此操作..
MainActivity mainActivity;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mainActivity = (MainActivity) activity;
mainActivity.TAG = getClass().getSimpleName();
}
希望它有所帮助。谢谢!! ; d
答案 1 :(得分:0)
只需从代码中删除super.onBackPressed();