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
What does getActivity() mean?
提问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:
两个可能的定义:
getActivity()
in aFragment
returns theActivity
theFragment
is currently associated with. (see http://developer.android.com/reference/android/app/Fragment.html#getActivity()).getActivity()
is user-defined.
getActivity()
在Fragment
返回Activity
的Fragment
当前关联。(参见http://developer.android.com/reference/android/app/Fragment.html#getActivity())。getActivity()
是用户定义的。
回答by AshuKingSharma
getActivity()
is used for fragment
. For activity
, wherever you can use this
, you can replace the this
in fragment
in similar cases with getActivity()
.
getActivity()
用于fragment
. 因为activity
,在任何可以使用的地方this
,您都可以将类似情况下的this
in替换为.fragment
getActivity()
回答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 Activity
to 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()
方法通常用于静态片段,因为关联的活动不会是静态的,并且非静态成员不能在静态成员中使用。