Меню параметров не инициализировано во фрагменте - PullRequest
0 голосов
/ 06 ноября 2018

У меня есть рамка, в которую я помещаю различные фрагменты. У некоторых отображается панель действий, у некоторых - нет. Но тот, что приведен ниже, не отображает панель действий, как другие.

public class AFragment extends Fragment {

View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_a, container, false);
    ButterKnife.bind(this, view);
    setHasOptionsMenu(true);
    return view;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_a, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.action_a) {
        //getFragmentManager().popBackStackImmediate();
    }
    return super.onOptionsItemSelected(item);
}

}

Этот фрагмент вызывается из предыдущего фрагмента, как

AFragment aFragment = new AFragment ();
getFragmentManager().beginTransaction()
                            .replace(R.id.fragment, aFragment , "fragment_a")
                            .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                            .addToBackStack(null)
                            .commit();

Кстати, я не показываю панель действий в предыдущем фрагменте.

В случае необходимости, styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppActionBar" parent="AppTheme">
    <item name="android:windowActionBarOverlay">true</item>
    <item name="windowActionBarOverlay">true</item>
</style>

1 Ответ

0 голосов
/ 06 ноября 2018

Для фрагментов вы должны убедиться, что setHasOptionsMenu() установлен в методе onCreate(), а теперь - onCreateView().

Затем обязательно установите панель инструментов (если она добавлена ​​в макет фрагмента) в качестве панели инструментов для действия.

public class AFragment extends Fragment {

    View view;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_a, container, false);
        ButterKnife.bind(this, view);
        //moved to onCreate() method
        //setHasOptionsMenu(true);

        //set toolbar as the default for the activity class if AppCompat
        ((AppCompatActivity) getActivity()).setSupportActionBar(/*your toolbar goes here*/);

        //set toolbar is activity class is not AppCompat
        getActivity().setActionBar(/*your toolbar goes here*/);
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_a, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_a) {
            //getFragmentManager().popBackStackImmediate();
        }
        return super.onOptionsItemSelected(item);
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...