Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does getActivity() mean?

Tags:

android

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;         }     }); 
like image 996
zoey Avatar asked Sep 26 '12 21:09

zoey


People also ask

What is getActivity ()?

getActivity() in a Fragment returns the Activity the Fragment is currently associated with. (see http://developer.android.com/reference/android/app/Fragment.html#getActivity()).

What does getActivity() return?

getActivity is a function, it returns an Activity. It's a function of a Fragment, and returns the Activity the fragment is attached to.

How do you use getActivity?

getActivity(); is a method of android Fragment, if you want to show dialog in your activity, just pass this of your activity instead of getActivity() . Show activity on this post. getActivity() is the member method of the super class Activity that you extend from . It is defined in the Activity class itself.

What is context android?

Definition. it's the context of current state of the application/object. It lets newly-created objects understand what has been going on. Typically, you call it to get information regarding another part of your program (activity and package/application).


2 Answers

Two likely definitions:

  • getActivity() in a Fragment returns the Activity the Fragment is currently associated with. (see http://developer.android.com/reference/android/app/Fragment.html#getActivity()).
  • getActivity() is user-defined.
like image 137
James McCracken Avatar answered Oct 10 '22 06:10

James McCracken


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

like image 32
3 revs, 2 users 75% Avatar answered Oct 10 '22 06:10

3 revs, 2 users 75%