What does getActivity() mean?
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?
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;
}
});
Solution 1:
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.
Solution 2:
getActivity()
is used for fragment
. For activity
, wherever you can use this
, you can replace the this
in fragment
in similar cases with getActivity()
.
Solution 3:
getActivity()- Return the Activity this fragment is currently associated with.