Reputation: 1622
I want to know how I can start an Activity after completing a task, in details:
In my class I have an AsyncTask
, after finish my task, I want to call activity2
private class AccessTokenGet extends AsyncTask<String, String, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... args) {
return true;
}
@Override
protected void onPostExecute(Boolean response) {
if (response) {
//call activity2
}
}
Upvotes: 1
Views: 1850
Reputation: 5505
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();
}
}
Upvotes: 2
Reputation: 39529
@Override
protected void onPostExecute(Boolean response) {
if (response) {
progress.hide();
final Intent intent = new Intent(this, Activity2.class);
activity1.startActivity(intent);
}
}
the only question here is where you get the reference to activity1 - there are some options here that depends on your project - e.g. you can pass it to the async task in the constructor.
PS: be careful with this uncehcked progress.hide(); - this might crash when someone sends your Activity to the background ( e.g. by pressing home ) before this
Upvotes: 0