【问题标题】:Android Studio: How to implement backbutton in ActionBar from FragmentsAndroid Studio:如何从 Fragment 在 ActionBar 中实现后退按钮
【发布时间】:2019-12-10 11:04:56
【问题描述】:

我在片段的 ActionBar 中实现后退按钮时遇到问题。由于这是一个片段,我无权访问 getSupportActionBar();每次我使用这个或类似的代码时:

ActionBar actionBar = getActivity().getActionBar();
          actionBar.setDisplayHomeAsUpEnabled(true); 

只会导致 NullPointerExceptions。

我在 StackOverflow 上查看了许多类似的问题,但其中大多数都指定给 ActivityAppCompatActivities,它们在 Fragments 中不起作用。使用

getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);

只会导致 NullPointerExceptions。其他类似的问题和答案没有帮助我解决这个问题,所以我不得不创建一个新主题。

这是我得到错误的地方:

public class ExampleFragment extends Fragment{
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();

        ActionBar actionBar = getActivity().getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true); //This results in NullPointerException

        inflater.inflate(R.menu.example_menu, menu);

        super.onCreateOptionsMenu(menu, inflater);
    }

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

【问题讨论】:

    标签: java android android-fragments android-actionbar


    【解决方案1】:

    您需要接收 supportActionBar。

    public class ExampleFragment extends Fragment {
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 
                                 @Nullable Bundle savedInstanceState) {
            setHasOptionsMenu(true);
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    
        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            ActionBar actionBar = ((AppCompatActivity)requireActivity()).getSupportActionBar();
            if (actionBar!=null) {
                actionBar.setDisplayHomeAsUpEnabled(true);
            }
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (item.getItemId() == android.R.id.home) {
                requireActivity().getSupportFragmentManager().popBackStack();
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
    

    【讨论】:

    • onCreateOptionsMenu 现在效果很好,谢谢!但是onOptionsItemSelected 似乎有问题,因为按下后退按钮时什么都没有发生,它甚至没有进入方法。有什么建议吗?
    • 也许,你在Activity中覆盖onOptionsItemSelected吗?如果 Activity 中的 onOptionsItemSelected 返回 true 则不会调用 Fragment 中的 onOptionsItemSelected。
    • 我已准备好回答您的其他问题。如果它回答对您有帮助,那么您可以批准并投票给答案。
    • 我在 Activity 中覆盖了 onOptionsItemSelected,所以我只是注意到其他 ActionBar 菜单项的功能更改为 onBackPressed,但不在预期的后退按钮上
    • 我想问题与 ActionBar 无关。你可以检查一下。将简单的按钮添加到片段。在按钮的 OnClickListner 中添加 {requireActivity().getSupportFragmentManager().popBackStack();}。如果单击 Button 片段后继续存在,则问题与 SupportFragmentManager 的使用有关。
    【解决方案2】:

    我没有让它工作,所以我改变了 View 的结构,并将它变成了一个 Activity,它在处理这个问题时有更多的支持。

    @Override
        protected void onCreate(@Nullable Bundle savedInstance){
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    
    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
    
            if (id == android.R.id.home) {
                finish();
            }
    
            return super.onOptionsItemSelected(item);
        }
    

    【讨论】:

      猜你喜欢
      • 2012-04-23
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      相关资源
      最近更新 更多