Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly start activity from PostExecute in Android?

Tags:

I have an AsyncTask, that fills a custom List with parsed data from Internet.

In PostExecute I fill that List and get it ready to transfer it to a new Activity.

I do it this way:

@Override protected void onPostExecute(List<VideoDataDescription> result)  {     super.onPostExecute(result);     MainActivity.progressDialog.dismiss();      context.startActivity(new Intent(context, ResultsQueryActivity.class));   } 

where context

    private Context context; 

In LogCat after executing this code I get a Java.lang.NullPointerException. Is this possible and correct to start an Activity as I do it?

UPD I have added

    private Context mContext;   public YoutubeAndYahooParser(Context context)  {     super();     this.mContext = context; } 

to initialize context and call

YoutubeAndYahooParser youtubeAndYahooParser = new YoutubeAndYahooParser(ResultsQueryActivity.this);                     youtubeAndYahooParser.execute("my string to pass in asynctak"); 

After this in PostExecute

Intent intent = new Intent(mContext, ResultsQueryActivity.class);  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  mContext.startActivity(intent);     

I added new flag because of I have got in LogCat the next:

*Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?*

Am I right?

like image 778
Eugene Shmorgun Avatar asked Feb 02 '12 18:02

Eugene Shmorgun


People also ask

How do I start the same activity again on android?

If you just want to reload the activity, for whatever reason, you can use this. recreate(); where this is the Activity. This is never a good practice. Instead you should startActivity() for the same activity and call finish() in the current one.

What is the method start activity () in android?

1.2. To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.

How can I get previous activity name in Android?

Intent intent = getIntent(); String activity = intent. getStringExtra("activity"); Now in the string activity you will get the name from which Activity it has came.


1 Answers

You should pass in the application context rather than a context from the local activity. I.e. use context.getApplicationContext() and save that in a local variable in your AsyncTask subsclass.

The code might looks something like this:

public class MyAsyncTask extends AsyncTask {      Context context;     private MyAsyncTask(Context context) {         this.context = context.getApplicationContext();     }      @Override     protected Object doInBackground(Object... params) {         ...     }      @Override     protected void onPostExecute(List<VideoDataDescription> result) {         super.onPostExecute(result);         MainActivity.progressDialog.dismiss();          context.startActivity(new Intent(context, ResultsQueryActivity.class));     } } 

you'd call it like this:

   new MyAsyncTask(context).execute(); 
like image 150
dhaag23 Avatar answered Sep 29 '22 23:09

dhaag23