1

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
        }
    }
OiRc
  • 1,602
  • 4
  • 21
  • 60
  • 1
    if the AsyncTask is an inner class, then call the Activity callback method in the Fragment that starts Activity2: http://codereview.stackexchange.com/questions/57543/why-does-the-new-adt-create-a-static-inner-class-fragment-by-default – EpicPandaForce Aug 15 '14 at 12:45
  • @Zhuinden ok thanks a lot , i understood. if i have a class that is not a fragment or something else, on which i extend `AsyncTask` , how i can start another `activity` from `onPostExecute` method? – OiRc Aug 15 '14 at 12:50
  • 1
    AsyncTask `onPostExecute()` calls `activity.goToNextActivity();` which is a Callback function you must implement yourself like in the example I linked, creates an intent `Intent intent = new Intent(this, NextActivity.class);` and starts activity `startActivity(intent);`. – EpicPandaForce Aug 15 '14 at 12:53
  • possible duplicate of [Starting Activity from Fragment causes NullPointerException](http://stackoverflow.com/questions/8748064/starting-activity-from-fragment-causes-nullpointerexception) – Shabbir Dhangot Aug 15 '14 at 12:55

2 Answers2

2

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();
    }
}
Anderson K
  • 5,445
  • 5
  • 35
  • 50
  • i start the aSyncTask from activity1, and i want to launch for the first time activity2 after completing this. – OiRc Aug 15 '14 at 13:12
  • @oIrC, I edited my answer to try to approach more clearly what I believe is the issue open here. – Anderson K Aug 15 '14 at 13:20
  • 1
    woooo.... don't like that pointer to the current activity (mActivity). Memory leak... – G. Blake Meike Aug 15 '14 at 13:47
  • @G.BlakeMeike,As a matter of organization at the level of architecture in projects when using AsyncTask these are always in a different package and use callback to communicate with other parts of the project. For a simple case like this, a simple check if the reference of Activity and still valid, is enough. – Anderson K Aug 15 '14 at 14:05
  • Not sure what that means... but the Activity cannot be GCed as long as the AT is holding a ref to it. – G. Blake Meike Aug 15 '14 at 15:11
  • Well, I do not see why it all, but ok! – Anderson K Aug 15 '14 at 15:15
0
@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

ligi
  • 39,001
  • 44
  • 144
  • 244