Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android equivalent of getActivity() from/in ActionBarActivity

Like my title says, i'm looking for an equivalent of getActivity() in my ActionBarActivity class in my Android project.

I want to pass an Activity parameter in AsyncTask declaration object, because i'm using an Activity object in my custom AsyncTask extended class

Here an example simplest code of my project

public class EventCreator extends ActionBarActivity {

private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_even_creator);
    View v = getLayoutInflater().inflate(R.layout.activity_even_creator,null);
    this.context = this.getBaseContext();

    final Button createButton = (Button)findViewById(R.id.createEventButton);
    createButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AsyncTask<Void,Void,Boolean> eventCreatorSend = new SendEvents(/* here need activity object */);
            eventCreatorSend.execute();
        }
    });

}

    class SendEvents extends AsyncTask<Void,Void,Boolean> {

        public Activity act;

        SendEvents(Activity a) {
            this.act = a;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            ((LinearLayout)act.findViewById(R.id.layout_loader_create_event)).setVisibility(View.VISIBLE);
        }

        @Override
        protected Boolean doInBackground(Void... params) {

            SystemClock.sleep(5000);
            return true;
        }

        @Override
        protected void onPostExecute(Boolean params) {
            if (params){

                ((LinearLayout)act.findViewById(R.id.layout_loader_create_event)).setVisibility(View.GONE);
                act.finish();
            }
            else {
                ((LinearLayout)act.findViewById(R.id.layout_loader_create_event)).setVisibility(View.VISIBLE);
                Toast.makeText(act,"Fail to send event",Toast.LENGTH_SHORT).show();
            }
        }


    };
}

In a time, i thought use getParent() from ActionBarActivity class, but it return a null object. So how to get the Activity object i want in ActionBarActivity class ?

like image 740
MrLeblond Avatar asked May 28 '15 02:05

MrLeblond


People also ask

What is getActivity () in android?

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.

What is the difference between getContext and getActivity?

What is the difference between getActivity and getContext? getContext() - Returns the context view only current running activity. getActivity()- Return the Activity this fragment is currently associated with. getActivity() can be used in a Fragment for getting the parent Activity of the Fragment .

What is difference between getActivity and requireActivity?

The only difference is requireActivity throw an IllegalStateException if the Activity is null. but getActivity return null when that Fragment is not attached to the Activity. requireActivity() returned exception and its message. Certainly, requireActivity() throws a more explicit exception.

What is the use of AppCompatActivity in Android?

Stay organized with collections Save and categorize content based on your preferences. Base class for activities that wish to use some of the newer platform features on older Android devices.


1 Answers

Try nameofactivity.this instead getActivity()

I always use getActivity() in Fragments Activities and .this in any other kind of Activity.

like image 109
frankie015 Avatar answered Sep 22 '22 12:09

frankie015