thang tran
thang tran

Reputation: 9

Callback and update data

I have 3 Activity

Activity 1 show listview

Activity 2 show detailItem (activity 3 will call in activity 2)

Activity 3 create new task

I want when create success in activity 3 then callback activity 1 and update data

Upvotes: 0

Views: 291

Answers (1)

Mahdi-Malv
Mahdi-Malv

Reputation: 19220

Not a perfect solution for very large projects, but:

You can use EventBus to send event to from a place to another.

First make the message class:

public static class MessageEvent { /* Additional fields if needed */ }

Send data from anywhere using:

 EventBus.getDefault().post(new MessageEvent());

And receive it by registering the activity to event bus:

 @Override
 public void onStart() {
     super.onStart();
     EventBus.getDefault().register(this);
 }

 @Override
 public void onStop() {
     super.onStop();
     EventBus.getDefault().unregister(this);
 }

Add this to activity to be notified when message was received:

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(MessageEvent event) {/* Do something */};

Add it to gradle using it's dependency code:

implementation 'org.greenrobot:eventbus:3.1.1'

Upvotes: 1

Related Questions