我有一个包含一些片段的滑动菜单,但我想用活动更改它,是否可能?
private void replaceFragment(int pos) {
Fragment fragment = null;
switch (pos) {
case 0:
fragment = new X();
break;
case 1:
fragment = new Y();
break;
case 2:
fragment = new Z();
break;
default:
fragment = new X();
break;
}
if (null != fragment) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_content, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
我不知道如何更改代码的最后部分。 - > FragmentManager和 - > FragmentTransaction。谢谢!
答案 0 :(得分:0)
是的,您可以使用Activity代替片段,但是 你有以下挑战。
更新您的代码
private void replaceFragment(int pos) {
Intent intent = null;
Context context = this;
switch (pos) {
case 0:
intent = new Intent(context, SecondActivity.class);
break;
case 1:
intent = new Intent(context, ThirdActivity.class);
break;
case 2:
intent = new Intent(context, FourthActivity.class);
break;
default:
intent = new Intent(context, DefaultActivity.class);
break;
}
if (intent != null) {
startActivity(intent);
}
}