2

在 selectItem() 方法中,将 .addToBackStack(null) 添加到我的事务中似乎会破坏切换按钮(点击时会显示导航抽屉的那个)。它只是在加载片段时完全消失(通过单击导航抽屉中的相应项目)。如果我注释掉该行,切换按钮将保留并且工作正常。

不过,我需要调用 addToBackStack ,因为我希望后退按钮功能适用于这些片段。为什么会导致这个问题?

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        Log.d(TAG, "Item " + position + " selected");
        selectItem(position);
    }
}

/** Swaps fragments in the main content view */
private void selectItem(int position) {
    // Create a new fragment and specify the planet to show based on position
    Fragment aboutFragment = new AboutFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    Log.d(TAG, "selectItem() with position  " + position);

    switch(position) {
        case 0:
            fragmentManager.beginTransaction()
                    .replace(R.id.contributionsFragmentContainer, contributionsList)
                    //.addToBackStack(null)
                    .commit();
            Log.d(TAG, "contributionsListFragment committed");
            break;
        case 1:
            fragmentManager.beginTransaction()
                    .replace(R.id.contributionsFragmentContainer, aboutFragment)
                    //.addToBackStack(null)
                    .commit();
            Log.d(TAG, "aboutFragment committed");
            break;

    }

    // Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(itemArray[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title) {
    if (title != null && getActionBar() != null) {
        getActionBar().setTitle(title);
    }
}

private void addDrawerItems() {
    itemArray = new String[] {"Home", "About", "Nearby", "Settings", "Feedback"};
    Log.d(TAG, "Adding to itemArray");
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, itemArray);
    mDrawerList.setAdapter(mAdapter);
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}

private void setupDrawer() {
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(R.string.title_activity_contributions);
    setContentView(R.layout.activity_contributions);
    Log.d("ContributionsActivity", "onCreate()");

    // Activity can call methods in the fragment by acquiring a reference to the Fragment from FragmentManager, using findFragmentById()
    contributionsList = (ContributionsListFragment) getSupportFragmentManager().findFragmentById(R.id.contributionsListFragment);

    //Set up navigation drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    //getSupportActionBar().setHomeButtonEnabled(true);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.drawer_list);
    addDrawerItems();
    setupDrawer();
    ...
}
4

0 回答 0