How to correctly start activity from PostExecute in Android?
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();
Android AsyncTask: start new Activity in onPostExecute()
Try this,
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
Intent intent = new Intent(MyAsyncTaskActivity.this, NextActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
}
Starting an Activity in onPostExecute
I don't see any problem with asynctask. Problem seems to be with your emulator. Try to restart it. Maybe this link helps you: EGL_emulation failed to establish connection to host - android
Is it possible to start an activity in onPostExecute?
use
startActivity(new Intent(Your_Current_Activity.this, ActionActivity.class));
OR
startActivity(new Intent(getApplicationContext(), ActionActivity.class));
instead of
startActivity(new Intent(this, ActionActivity.class));
for starting new activity from onPostExecute
Using AsyncTask to start activity
Yes, you can start activity from AsyncTask's sub class. See below:
@Override
protected void onPostExecute(Boolean result) {
Toast.makeText(activity, Boolean.toString(result), Toast.LENGTH_LONG).show();
activity.startActivity(new Intent(activity, BuiltInCamera.class));
}
After making this change, make sure you do remove startActivity from OnClickListener
Android Start an activity in preExecute and finish it in postExecute during running Async Task
That's just not how Android works.
There is always one Activity active at a time, and when you start an AsyncTask, you do so in the context of the current Activity.
So, while you technically could start an activity from the AsyncTask, it's parent context (the Activity that started the AsyncTask) would become inactive. The AsyncTask would continue to run (this is actually a big problem with AsyncTasks), but would likely crash - and even if not, the behavior would be undefined.
Shubham Nandanwar's answer looks like it should work, but is not the correct way to approach this. You should reconsider what you are trying to do in the context of the way that Activities and their lifecycles are supposed to work.
I'd suggest simply launching the Activity that you desire, and then use an AsyncTask in that activity to do whatever work needs to be done, and finish the activity from the activity itself when the work is done.
Better yet, find a better way to do background processing (e.g. RxJava), and do away with the AsyncTask.
How to start new intent from PostExecute in Android?
You are returning null in doinbackground() method instead return valid user and in onPostExecute
protected void onPostExecute(String validUser) {
pDialog.dismiss();
if(validUser)
{
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
}
params.clear();
}
}
Start an Activiy from an AsyncTask (PostExecute) in Android
Remember, you have to declare each activity in the AndroidManifest.xml
file :)
<activity android:name=".DownloadImages" />
start another activity from an AsyncTask
if your class is inner
public class StackQuestion extends Activity {
@Override
protected void onCreate( final Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
final MyAsyn mMyAsyn = new MyAsyn( this );
mMyAsyn.execute();
}
public class MyAsyn extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground( final Void ... params ) {
return null;
}
@Override
protected void onPostExecute( final Void result ) {
super.onPostExecute( result );
startActivity( new Intent( "your intent here" ) );
}
}
}
if it is in a separate file
public class StackQuestion extends Activity {
@Override
protected void onCreate( final Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
final MyAsyn mMyAsyn = new MyAsyn( this );
mMyAsyn.execute();
}
}
public class MyAsyn extends AsyncTask<Void, Void, Void> {
private final Activity mActivity;
public MyAsyn( final Activity mActivity ) {
this.mActivity = mActivity;
}
@Override
protected Void doInBackground( final Void ... params ) {
return null;
}
@Override
protected void onPostExecute( final Void result ) {
super.onPostExecute( result );
this.mActivity.startActivity( new Intent( "your intent here" ) );
}
}
Edited:
public class Activity1 extends Activity {
@Override
protected void onCreate( final Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
final AccessTokenGet mAccessTokenGet = new AccessTokenGet( this );
mAccessTokenGet.execute();
}
}
public class Activity2 extends Activity {
@Override
protected void onCreate( final Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
//Your code here
}
}
public class AccessTokenGet extends AsyncTask<String, String, Boolean> {
private final Activity mActivity;
public AccessTokenGet( final Activity mActivity ) {
this.mActivity = mActivity;
}
@Override
protected Boolean doInBackground( final String ... args ) {
return true;
}
@Override
protected void onPostExecute( final Boolean response ) {
if ( response ) {
this.mActivity.startActivity( new Intent( this.mActivity.getBaseContext(), Activity2.class ) );
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
}
Restart Activity onPostExecute
You can Simply use
startActivity(getIntent());
finish();
to refresh an Activity from within itself.
Alternatively you can call recreate()
to refresh the activity.
Related Topics
Why Is the Android Test Runner Reporting "Empty Test Suite"
What Are the Differences Among Internal Storage, External Storage, Sd Card and Removable Storage
How to Know When Tts Is Finished
Why Is Android Webview Refusing User Input
Is Google's Android Opengl Tutorial Teaching Incorrect Linear Algebra
Android Copy/Paste from Clipboard Manager
Find Out the Running Process Id by Package Name
How to Retrieve the Data from Asynctasks Doinbackground()
Animation in Notification Bar Custom View
How to Disable All Views Inside the Layout
Difference Between Content_Main.Xml and Activity_Main.Xml
Android App Crashes with Sigabrt Signal 6 Only While Eclipse Debugging
Receive Mms Messages in Android Kitkat
Broadcast Receiver to Detect Application Start