Android getActivity() 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12610995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:11:58  来源:igfitidea点击:

What does getActivity() mean?

android

提问by zoey

What does getActivity()mean? I saw in somewhere, they wrote MainActivity.this.startActionMode(mActionModeCallback)instead of getActivity(). could someone explain what this two lines mean?

什么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;
        }
    });

回答by James McCracken

Two likely definitions:

两个可能的定义:

回答by AshuKingSharma

getActivity()is used for fragment. For activity, wherever you can use this, you can replace the thisin fragmentin similar cases with getActivity().

getActivity()用于fragment. 因为activity,在任何可以使用的地方this,您都可以将类似情况下的thisin替换为.fragmentgetActivity()

回答by Trojan Horse

getActivity()- Return the Activity this fragment is currently associated with.

getActivity()- 返回当前与此片段关联的 Activity。

回答by Vamen95

I to had a similar doubt what I got to know was getActivity()returns the Activityto which the fragment is associated.

我有一个类似的疑问,我所知道的是getActivity()返回 Activity与片段相关联的对象。

The getActivity()method is used generally in static fragment as the associated activity will not be static and non static member cannot be used in static member.

getActivity()方法通常用于静态片段,因为关联的活动不会是静态的,并且非静态成员不能在静态成员中使用。

I used <code>getActivity()</code>here to get non-static activity to which the the placeholder fragment is associated.

我在这里使用 <code>getActivity()</code>来获取与占位符片段相关联的非静态活动。