Dbucha01
Dbucha01

Reputation: 55

asyncTask from one activity running in another activity android

Im just wondering what would be the best and possibly easiest way to do this. I have two activites LoginAcitivty and Main Activity.

Ive coded an AsyncTask in my MainActivity as an inner class which sends updates to a web service. When i click the logout button of the MainActivity this returns the app to the Login Activity. Is it possible to still run the ASyncTask even though there is a different activity running or is there another way to do something like this?

Any suggestions would be greatly appreciated Thanks

Upvotes: 1

Views: 1566

Answers (2)

AMC
AMC

Reputation: 130

The Asynctask is tied to the "Entity" that created it, in your case it would be the MainActivity, so it will not survive the destroy of your activity (I trust you call the finis() method of the main activity once the user logs out) What you can do is use a service that runs in background and use the async task to poll your server:

The service shall look like this:

 public class PollService extends Service {

    @Override
    public void onCreate() {
      super.onCreate();    
    }

    public void onStart(Intent intent, int startId) {
      (new PollAsyncTask(this)).execute();
    }

    //callback used to retrieve the result from the asynctask
    void callBack(String result) {
      //here is your logic, taking the result back from the async task
      //eventually re-run the asynctask
      (new PollAsyncTask(this)).execute();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

The AsyncTask shall look like this:

 private class PollAsyncTask extends AsyncTask<String, Void, String> {
    private PollService caller;
    PollAsyncTask(PollService caller) {
         this.caller = caller;
    } 

    @Override
    protected String doInBackground(String... params) {
        //do your polling here and return something meaningful to the service,
        return SOMETHING_REFERRING_TO_THE_1_OF_3;
    }

    @Override
    protected void onPostExecute(String result) {
        //Give the result back to the caller:
        this.caller.callBack(result);
    }

    @Override
    protected void onPreExecute() {//nothing special here}

    @Override
    protected void onProgressUpdate(Void... values) {//nothing special here}

  }

That way your async task will poll your server whatever activity is currently in foreground. The service shall be started by your first activity when it is run the first time (i.e. in the onCreate method):

 @Override
 public void onCreate(Bundle savedInstanceState) {
     if (savedInstanceState==null) {//only the first time
          Intent serviceIntent = new Intent();
          serviceIntent.setAction("com.yourcompany....PollService");
          startService(serviceIntent);
     }

 }

Hope this helps.

Upvotes: 1

Alberto
Alberto

Reputation: 377

For what I understand, you have an inner Class in your MainActivity. So just make your AsyncTask in a separate Class and you can call it from both Activites.

Like: new YourAsyncTask().execute();

Greetings.

Upvotes: 0

Related Questions