据我所知,每个Fragment都有自己的backstack,这与FragmentActivity的所有片段共享。假设您必须管理多个选项卡,并且每个选项卡都可以浏览多个片段。假设您要“记录”每个选项卡的导航历史记录,因此在片段之间切换将允许您返回到您正在查看的片段。有可能实现吗?我是否需要将每个标签链接到片段活动?在这种情况下,如何管理FragmentActivity之间的切换?
答案 0 :(得分:7)
没有“标准”的解决方法,因为不鼓励这种设计风格。但是,我找到了一种方法使其工作:您将手动设计导航。
您的应用程序应该有一个Activity,一个FragmentActivity。它有一个FragmentTabHost,可以容纳每个TabFragments。
TabFragment是我创建的抽象类,用于表示TabSpec中的选项卡。它将管理选项卡中的片段导航和交换。
然后,您创建的各个片段可以在TabFragment对象中交换。这是代码:
活动
public class MainActivity extends FragmentActivity {
private FragmentTabHost tabHost;
//(TabFragment)s will set this property when created so the Activity can communicate with it
public TabFragment activeFragment;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//create tabHost based on .xml layout
tabHost = (FragmentTabHost)findViewById(R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
//add each of your tabs to the tabHost. These are all (TabFragment)s
tabHost.addTab(tabHost.newTabSpec("New Tab").setIndicator("New Tab"),
ExampleTabFragment.class, null);
}
/*override the onBackPressed method, so that your application does not close every
time the user navigates back. Instead, calls the activeFragment to determine behavior*/
@Override
public void onBackPressed() {
activeFragment.onBackPressed();
}
//method for TabFragment to call when the user navigates out of the app
public void close(){
super.onBackPressed();
}
}
<强> TabFragment 强>
public abstract class TabFragment extends Fragment {
@Override
public void onResume(){
//sets the property in the Activity so we can reference this from it.
((MainActivity) getActivity()).activeFragment=this;
super.onResume();
}
//this will be the method called when the back button is pressed. It will navigate.
public abstract void onBackPressed();
}
TabFragment实例
在TabFragment的实例内部应该是可以附加子Fragment的FrameLayout。第一次单击选项卡时,它将启动onCreate()
中指定的片段。从另一个选项卡切换回它后,它将恢复上次显示的片段。如果需要分层导航,则应使用onBackPressed()方法导航回片段。我使用了一个byte属性(tabContentIndex
)来确定如何导航。如果您添加一个构造函数来获取此TabFragment的实例,则Fragments可以将自己交换为其他片段。他们将通过访问start(Example)Fragment()
方法来完成此操作。请记住,“后退”按钮最终必须退出应用程序。
public class NewTrailTabContent extends TabFragment {
//to determine what child Fragment is active
byte tabContentIndex;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//a simple FrameLayout in this case. Child Fragments will be attached.
return inflater.inflate(R.layout.example_fragment, container,
false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
//The tab starts with this Fragment
startDiologFragment();
super.onCreate(savedInstanceState);
}
public void startExampleFragment(){
/*Fragment's constructor allows us to reference the parent to navigate. In effect, this
Fragment will be able to call these navigation methods.*/
ExampleFragment newFragment = new ExampleFragment(this);
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
//this Resource is the FrameLayout
fragmentTransaction.replace(R.id.example_contentpane,
newFragment);
fragmentTransaction.commit();
//this is set so the onBackPressed() method knows how to operate.
tabContentIndex =0;
}
public void startTestFragment(){
Fragment testFragment = new TestFragment(this);
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.example_contentpane,
testFragment);
fragmentTransaction.commit();
//different contentIndex
tabContentIndex = 1;
}
//this method called by the Activity
@Override
public void onBackPressed() {
//this will close the app because we are at the top of the hierarchy
if(tabContentIndex==0){
((MainActivity)getActivity()).close();
//this will switch to the original content fragment.
}else if(tabContentIndex==1||tabContentIndex==2){
startExampleFragment();
}
}
}
答案 1 :(得分:1)
这不是一个很好的导航方法。我会推荐你的。看看Android设计模式并重新思考你的流程。
但是,是的,如果不编写自己的FragmentManager
,则需要多个FragmentActivities
看看使用TaskStacks但这只能在HoneyComb上运行。因此,我建议您重新考虑导航或仅构建HC +应用程序。
我知道我不会给你很多代码来真正解决你的问题。但请好好阅读Android Navigation它解释了时间和祖先导航,这是从Android 3.0 +“正式”添加的
答案 2 :(得分:1)
FragmentTransaction有一个方法addToBackStack()。
打开新片段this方式:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
使用此方法,当用户按下后退按钮时,它会显示当前标签的上一个片段,&amp;当片段堆栈变为空的当前选项卡时,它将退出应用程序。
addToBackStack()方法将此事务添加到后台堆栈。这意味着事务将在提交后被记住,并在稍后从堆栈中弹出时将反转其操作。