Backstack安卓片段导航

时间:2014-12-11 07:34:59

标签: android android-fragments fragment-backstack

这是我使用导航抽屉显示碎片的方式:

private void displayView(int position) {
            // update the main content by replacing fragments
            Fragment fragment = null;
            switch (position) {
            case 0:
                fragment = new Fragment0();
                break;
            case 1:
                fragment = new Fragment1();
                break;
            case 2:
                fragment = new Fragment2();
                break;
            case 3:
                fragment = new Fragment3();
                break;
            case 4:
                fragment = new Fragment4();
                break;
            case 5:
                fragment = new Fragment5();

                }

            default:
                break;
            }

            if (fragment != null) {
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment)
                        .addToBackStack(null)
                        .commit();

                // update selected item and title, then close the drawer
                mDrawerList.setItemChecked(position, true);
                mDrawerList.setSelection(position);
                setTitle(navMenuTitles[position]);
                mDrawerLayout.closeDrawer(drawerll);
            } else {
                // error in creating fragment
                Log.e("MainActivity", "Error in creating fragment");
            }
        }

假设用户导航到 Fragment 2-> Fragment 5-> Fragment3.

如果用户点击 back button ,我需要拥有descending order,并且在每个片段上,当他访问时,我正在显示片段的name特别是fragment即:

Fragment 3-> Fragment 5-> Fragment2.

这就是我的尝试:

第一种方法:

FragmentManager fm = getSupportFragmentManager();
int count = fm.getBackStackEntryCount();
for(int i = 0; i < count; ++i) {
    fm.popBackStackImmediate();
}

第二种方法:

fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

我无法让这个工作**(即..我无法将其移回到我来自**的片段)并且无法根据backstack显示片段的名称< / p>

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:1)

您的导航工作正常我猜您遇到的问题是您可以使用的问题

public class SampleFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

                // Creating view correspoding to the fragment
        View v = inflater.inflate(R.layout.fragment_layout, container, false);

        // Updating the action bar title
        getActivity().getActionBar().setTitle("Screen name");


        return v;
    }
}

在每个片段中。每当你背压tommemt的onCreateView方法(从backstack弹出的那个)被调用时,tittle将再次设置替换旧的tittle。

答案 1 :(得分:0)

请试试这个:

在包含片段的活动中,覆盖onbackPressed()方法,

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
         getSupportFragmentManager().popBackStack();
         getSupportFragmentManager().beginTransaction().commit();
    }
    else {
        super.onBackPressed();
    }
}

答案 2 :(得分:0)

1。处理回来

您可以在getSupportFragmentManager().popBackStack()中使用onBackPressed方法:

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
         getSupportFragmentManager().popBackStack();
         getSupportFragmentManager().beginTransaction().commit();
    }
    else {
        super.onBackPressed();
    }
}

不要忘记在BackStack中添加片段,如:

transaction.addToBackStack(null);

2。显示名称

要在操作栏中显示当前片段名称,您可以在片段onResume上获取它:

@Override
protected void onResume() {
    super.onResume();

    ((FrgmentActivtiyName)getActivity).changeTitle("title");
}

定义一个在FrgmentActivity的操作栏中设置标题的方法:

public void changeTitle(String titleToSet) {
   // set title as titleToSet
}

希望它有助于ツ

答案 3 :(得分:-1)

if (fragment != null) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();
        menuList.setItemChecked(position, true);
        menuList.setSelection(position);
    }