android – 在popBackStack()之后将工具栏更改为抽屉/菜单图标?

我有一个带有MainActivity的
Android应用程序,可以在按下按钮时将内容区域与片段交换.使用当前代码(如下所示),它可以成功

>使用新片段交换内容区域
>在工具栏中显示“向上导航”(后退按钮)
>导航“向上”/“返回”(恢复内容区域的先前状态)

…但是工具栏本身不会更新以在其返回其原始状态时显示“抽屉”/“菜单”(汉堡包)按钮(getSupportFragmentManager().getBackStackEntryCount()== 0).我想我需要在我的活动上实现FragmentManager.OnBackStackChangedListener,尽管我的尝试并没有那么成功.我也看过提到syncState(),虽然我也没有太多的运气.

在交换和弹出片段后,如何将工具栏主页按钮返回到“抽屉”/“菜单”外观和功能?

我的代码可以在下面找到.试图在分享之前简化它,希望我没有削减任何重要的东西.

MainActivity.java:

package me.stashhh.stashboxes;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import me.stashhh.stashboxes.model.CountryManager;


public class MainActivity extends ActionBarActivity {
    private Toolbar toolbar;
    private ActionBarDrawerToggle mDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // normal onCreate stuff
        super.onCreate(savedInstanceState);

        // The ContentView that we're referencing
        setContentView(R.layout.activity_main);

        // Toolbar setup
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }

        // Drawer setup
        DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                toolbar,
                R.string.action_drawer_open,
                R.string.action_drawer_close
        );
        mDrawerLayout.setDrawerListener(mDrawerToggle); // REQUIRED FOR ARROW ANIMATION. Fml.
        mDrawerToggle.syncState();


        // Listen for changes in the back stack
        getSupportFragmentManager().addOnBackStackChangedListener(this);
    }

    public void onBackPressed() {
        // Catch back action and pops from backstack
        // (if you called previously to addToBackStack() in your transaction)
        if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
            getSupportFragmentManager().popBackStack();
        } else {
            // Default action on back pressed
            super.onBackPressed();
        }
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // Get item selected and deal with it
        switch (item.getItemId()) {
            case android.R.id.home:
                //called when "up" (back button) is called in toolbar
                onBackPressed();
                return true;
        }
        return true; // default return since 'boolean'; not sure if proper?
    }



    /**
     * Called when the user clicks the "View Stashboxes" button
     */
    public void viewStashboxes(View view) {
        // update the main content by replacing fragments
        Fragment stashboxesFragment = new StashboxesFragment();

        Bundle args = new Bundle();
        args.putString(StashboxesFragment.ARG_TEST_STRING, "Testing 1.. 2.. 3..");
        stashboxesFragment.setArguments(args);

        FragmentManager fragmentManager = getSupportFragmentManager();

        // Add this transaction to the back stack...
        // http://developer.android.com/training/implementing-navigation/temporal.html#back-fragments
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.content_frame, stashboxesFragment, "stashboxesFragment");
        fragmentTransaction.addToBackStack("stashboxesFragment");
        fragmentTransaction.commit();

        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            // If drawer is open, we have to close it...
            DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer); // The drawer layout
            LinearLayout mDrawerList = (LinearLayout) findViewById(R.id.left_drawer); // The drawer "list" itself
            mDrawerLayout.closeDrawer(mDrawerList); // Close the drawer list
        }
    }

    /**
     * Fragment that appears in the "content_frame"
     */
    public static class StashboxesFragment extends Fragment {
        public StashboxesFragment() {
            // empty constructor required for fragment subclasses
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // REDACTED CODE FOR SIMPLICITY.
            // inflate a layout in the main content area.

            return rootView;
        }
    }
}

我是Android开发的新手,所以任何其他批评你也可能会受到赞赏.谢谢.

最佳答案 这对我有用:请更新你的onBackPressed()

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub      
    if(frgManager.getBackStackEntryCount()>0){      
           getSupportFragmentManager().popBackStack();      
           // shows the drawer icon when on moving back 
            mDrawerToggle.setDrawerIndicatorEnabled(true);
            drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            getSupportActionBar().setDisplayShowHomeEnabled(false);
            getSupportActionBar().setHomeButtonEnabled(false);     
        }else{
            super.onBackPressed();
        }
    mDrawerToggle.syncState();
}
点赞