1

1 つのアクティビティと 5 つのフラグメントがあります。オプション メニューにナビゲーション ドロワー ボタンがあり、フラグメントをナビゲートするのに役立ちます。しかし、1 つのフラグメントで、それを戻るボタンに変更したいと考えています。アクティビティ オプション メニューも機能しており、ナビゲーション ドロワーが表示されます。これを停止するには?

これが私の引き出しです

主な活動

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.container, new SettingsFragment());
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
            break;       
        case android.R.id.home:
            mDrawerLayout.openDrawer(GravityCompat.START);
            break;
        default:
            break;
    }
    return super.onOptionsItemSelected(item);
}

断片

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            getFragmentManager().popBackStack();
            return true;
        default:
            break;
    }
    return super.onOptionsItemSelected(item);
}

実際、両方のオプションメニューでオプションメニューのホームコードを選択すると、フラグメントオプションメニューのみが機能するようになります。どうすれば止められますか?助けてください

4

1 に答える 1

1

First, in your activity, create a flag to check whether home button as back button or not: isBackShow = false. Then, in onOptionsItemSelected:

 case android.R.id.home: // show drawer or pop backstack base on flag
        if(isBackShow)
        getFragmentManager().popBackStack();
        mDrawerLayout.openDrawer(GravityCompat.START);
        return true;

In your SettingsFragment, you can change flag of father Activity as below in onCreateView or in somewhere you want:

if(getActivity() instanceof YourFatherActivity){
        ((YourFatherActivity)getActivity()).isBackShow = true;
        // You can also change drawer icon to back icon here.
}

I'm not check the code however, AFAIK this code will work. Hope this help.

于 2015-07-17T10:08:58.810 に答える