在互联网上找不到答案后,现在进入我的第一个问题。通常这应该是一件非常容易的事情,但我花了好几个小时,所以如果这是一个初学者问题,请原谅。此外,我想确保我在花更多天时间之前走的路是正确的,只是为了看看最终它是否像我想要的那样工作,我在 atm 上实现它的方式。
问题:
我的应用程序有 3 个选项卡,其中包含 3 个 fragment 作为内容,每个选项卡一个。我使用 Tabs 是因为我询问的所有用户都想要 Tabs 而不是 Navigation Drawer,而且我听取了用户的意见,而不是谷歌(抱歉)。此外,这 3 个选项卡具有非常不同的“角色”(语言指南、货币转换器和用于自己的短语的自己的笔记本等),并且以这种方式拆分它是有意义的(imo)。如果有人有其他想法,请继续,我开放 :-)
到目前为止,选项卡导航按预期工作。但现在我想为单个选项卡构建进一步的导航,但我不确定该走哪条路……像这样的简单事情有很多方法……
第一个选项卡有一些图像按钮可以进一步导航到此部分(参见屏幕截图 1),以在语言部分中选择一个类别。当然,这意味着选项卡应该保留在原位,以便能够轻松切换到转换器,即,只有选项卡 1 中的 fragment 应该切换到新 fragment (参见屏幕截图 2),依此类推。您在下面的屏幕截图中看到的按钮具有将当前 fragment 更改为新 fragment 的点击监听器(如“每日”)
为此,我打开另一个 fragment 并将其添加到后台堆栈。也许你已经猜到现在会发生什么。如果我按下后退按钮,我当然会回到第一个 fragment 。当我在选项卡 1 上打开的 fragment 内切换选项卡时,问题就开始了。添加的 fragment 仍在后台堆栈中,这意味着如果我在选项卡 2 或 3 上按后退按钮,显示的内容会以某种方式与其他 fragment 混淆。见截图 3
我是不是在这里犯了一个错误,或者我可以简单地告诉后台堆栈在切换选项卡时“重置”,这样这个问题就不会发生了吗?
感谢您提供信息,对于语言和拼写错误,我们深表歉意,英语不是我的主要语言。
Tablistener 类切换标签 fragment :
public class MyTabListener implements ActionBar.TabListener {
Fragment fragment;
public MyTabListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// nothing done here
}
第一个 fragment 中的 fragment 类如下所示(非完整代码):
public static class FragmentTab1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
final View view = inflater.inflate(R.layout.tab, container, false);
ImageButton catBtn_Daily = (ImageButton) view.findViewById(R.id.catBtn_Daily);
ImageButton catBtn_OnTheRoad = (ImageButton) view.findViewById(R.id.catBtn_OnTheRoad);
ImageButton catBtn_Shopping = (ImageButton) view.findViewById(R.id.catBtn_Shopping);
ImageButton catBtn_Restaurant = (ImageButton) view.findViewById(R.id.catBtn_Restaurant);
ImageButton catBtn_Romance = (ImageButton) view.findViewById(R.id.catBtn_Romance);
ImageButton catBtn_Emergency = (ImageButton) view.findViewById(R.id.catBtn_Emergency);
// attach an OnClickListener
catBtn_Daily.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create new fragment and transaction
Fragment newFragment = new FragmentTab4();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.setTransition(4097);
transaction.commit();
}
});
主要 Activity :
public class MainActivity extends Activity {
ActionBar.Tab tab1, tab2, tab3;
Fragment fragmentTab1 = new FragmentTab1();
Fragment fragmentTab2 = new FragmentTab2();
Fragment fragmentTab3 = new FragmentTab3();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_test);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tab1 = actionBar.newTab().setText("Home");
tab2 = actionBar.newTab().setText("Calculator");
tab3 = actionBar.newTab().setText("Notebook");
tab1.setTabListener(new MyTabListener(fragmentTab1));
tab2.setTabListener(new MyTabListener(fragmentTab2));
tab3.setTabListener(new MyTabListener(fragmentTab3));
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
还不能发布图片,rep 太低....
请看这里(第三张图叫Untitled-3.png)
屏幕 1:https://www.dropbox.com/s/3av3pf6a17p2w42/Untitled-1.png
屏幕 2:https://www.dropbox.com/s/3av3pf6a17p2w42/Untitled-2.png
屏幕 3:https://www.dropbox.com/s/3av3pf6a17p2w42/Untitled-3.png
最佳答案
您最好完全不使用 fragment 返回堆栈。您可以维护自己的堆栈并覆盖 onBackPressed()
以提供适当的行为。
@Override protected void onBackPressed()
{
if(myStack.isEmpty())
super.onBackPressed(); // default handling finishes the activity
else
{
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, myStack.pop());
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
transaction.commit();
}
}
关于android - 切换选项卡时重置 fragment 后退堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24590211/