什么时候btn
点击会显示 Fragment 1,当我按下btn2
Fragment 2 时会显示并且我相信 Fragment 1 被放置在 backstack 中。第二次,当我点击 时btn
,我想显示它以前的状态。我将包含让我非常困惑的代码,我不知道该怎么做。
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String str = "frag1";
Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.child_fragment, videoFragment, str);
transaction.addToBackStack(null);
transaction.commit();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String str2 ="frag1";
String str3 ="frag2";
//FragmentManager.BackStackEntry backEntry=
//getFragmentManager().getBackStackEntryAt(getActivity().getFragmentManager().
//getBackStackEntryCount()-1);
//String str3 = backEntry.getName();
//Fragment fragment= getFragmentManager().findFragmentByTag(str3);
Fragment videoFragment = new CopyOfVideoPlayerFragment();
FragmentTransaction transaction2 = getFragmentManager().beginTransaction();
transaction2.addToBackStack(str2);
transaction2.replace(R.id.child_fragment, videoFragment, str3);
transaction2.commit();
}
});
令人困惑的代码并且不起作用:
FragmentManager.BackStackEntry backEntry=getFragmentManager().getBackStackEntryAt(getActivity().getFragmentManager().getBackStackEntryCount()-1);
String str3 = backEntry.getName();
Fragment fragment= getFragmentManager().findFragmentByTag(str3);