getActivity() 是什么意思?

新手上路,请多包涵

getActivity() 是什么意思?我在某处看到,他们写了 MainActivity.this.startActionMode(mActionModeCallback) 而不是 getActivity() 。有人能解释一下这两行是什么意思吗?

   someView.setOnLongClickListener(new View.OnLongClickListener() {
        // Called when the user long-clicks on someView
        public boolean onLongClick(View view) {
            if (mActionMode != null) {
                return false;
            }

            // Start the CAB using the ActionMode.Callback defined above
            mActionMode = getActivity().startActionMode(mActionModeCallback);
            view.setSelected(true);
            return true;
        }
    });

原文由 zoey 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 719
2 个回答

getActivity() 用于 fragment 。 For activity , wherever you can use this , you can replace the this in fragment in similar cases with getActivity()

原文由 AshuKingSharma 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题