2

I just want to ask what is the best approach for me to solve the following problem. The situation is like.

1.) I have ActivityA & ActivityB.

2.) The User process something (AsyncTask) in ActivityA, now while user is waiting, he went to ActivityB.

3.) So while user is in ActivityB, the task was successful done in ActivityA. How would i notify ActivityB that the task was already done in ActivityA?

jhaypee
  • 71
  • 1
  • 9

5 Answers5

2

Sounds like some kind of event bus is what you may be looking for.

Activity A's AsyncTask when complete will create an Event and anyone who Subscribes to that event type will be notified.

Check out Otto: http://square.github.io/otto/

Razz
  • 220
  • 1
  • 6
  • 1
    Agreed. Though I would recommend EventBus (faster): https://github.com/greenrobot/EventBus – Knossos May 07 '15 at 14:04
  • 1
    I would love much to try this solution, but it looks advance to me, but for now, i have used LocalBroadcastManager – jhaypee May 07 '15 at 14:53
1

You can use LocalBroadcastManager. When your AsyncTask in ActivityA is done. Send a Broascast that ActivityB can receive. Then ActivityB will know The task was already done.

ActivityA:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_a);

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(ActivityA.this,ActivityB.class));
            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {

                    //do some long work
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void aVoid) {
                    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(new Intent("ACTION_TASK_DONE"));
                }
            }.execute();
        }
    });


}

ActivityB:

private BroadcastReceiver broadcastReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_a);

    broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(ActivityB.this,"Task done!" , Toast.LENGTH_SHORT).show();
        }
    };
    LocalBroadcastManager.getInstance(getApplicationContext())
            .registerReceiver(broadcastReceiver, new IntentFilter("ACTION_TASK_DONE"));


}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (broadcastReceiver != null) {
        LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(broadcastReceiver);
    }
}
Leo Lin
  • 681
  • 5
  • 8
0

You can use onPostExecute method to notify the user that the task is completed. Add the given method in your Async task -

protected void onPostExecute(Long result) {
     //notify the user
}

This method will be called when the Async task is completed.

Confuse
  • 5,646
  • 7
  • 36
  • 58
0

In your post execute Intent your activities from A to B

protected void onPostExecute(Long result) {
 Intent intent=new Intent(A.this,B.class);
 startactivities(intent);
}

Or if you want to intent in doinbackground..use runonUITheread

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
  • i think this code works ASSUMING i want to Run the activityB when actvityA is done, but if im running AcitivtyB , WHILE waiting for ActivityA, this doesnt work. – jhaypee May 07 '15 at 14:29
0

you can use BroadcastReceiver in activity B and call "sendBroadcast(Intent)" from onPostExecute to notify in activity B

Community
  • 1
  • 1
Ali Ahmad
  • 1,351
  • 9
  • 11