Android - What Is The Best Practice For Passing Back Information From An Activity Without Onactivityresult()?
Solution 1:
How about a local broadcast? You can find the idea of broadcast in this document. You need local broadcast and it is preferred if you want to pass data within your app only.
Android apps can send or receive broadcast messages from the Android system and other Android apps, similar to the publish-subscribe design pattern. These broadcasts are sent when an event of interest occurs. For example, the Android system sends broadcasts when various system events occur, such as when the system boots up or the device starts charging. Apps can also send custom broadcasts, for example, to notify other apps of something that they might be interested in (for example, some new data has been downloaded).
Solution 2:
You can use Handler to pass the Message in Activity and then You have to update RecyclerView. Like,
1) In Activity.
publicstaticHandlermHandler=newHandler(newHandler.Callback() {
@OverridepublicbooleanhandleMessage(Message msg) {
if(msg.what == 1223){
//update RecyclerView
}
returnfalse;
}
});
2) pass message When you want to update RecyclerView
Messagemsg=newMessage();
msg.what = 1223;
Activity1.mHandler.sendMessage(msg);
Solution 3:
You can use EventBus
to handle it.
Define a class for your event
publicstaticclassMyEvent {
intevent;
/* define your fields */
}
And prepare your subscriber in main activity
@Subscribe(threadMode = ThreadMode.MAIN)publicvoidonMyEvent(MyEvent myEvent) {
switch(myEvent.event) {
/* Do what you need */
}
};
Now where you need to make change, call your subscriber like this:
MyEvent myEvent = new MyEvent();
myEvent.event = 1;
EventBus.getDefault().post(myEvent);
You can read more about EventBus in here
Solution 4:
If you were using RxJava2
, RxAndroid
. Then you could try this.
Create a Bus:
publicfinalclassRxBus{ privatestaticfinal BehaviorSubject<Object> behaviorSubject = BehaviorSubject.create(); publicstatic BehaviorSubject<Object> getSubject() { return behaviorSubject; } }
In your WaitingActivity where you want to receive data(where you want not to use
onActivityResult
in your case)Disposable disposable = RxBus.getSubject(). subscribeWith(newDisposableObserver<Object>() { @OverridepublicvoidonNext(Object o) { if (o instanceofDataObject) { //((DataObject) o).getValue(); } } @OverridepublicvoidonError(Throwable e) { } @OverridepublicvoidonComplete() { } }); });
In your activity where you want to send data
RxBus.getSubject().onNext(dataObject); startActivity(new Intent(CurrentActivity.class, WaitingActivity.class));
Finally don't forget to dispose your disposable to avoid memory leaks in your WaitingActivity
@OverrideprotectedvoidonDestroy() { super.onDestroy(); disposable.dispose(); }
Solution 5:
Your data should be separate from view, in model. If some other activity changes data ideally recycler view must be updated from there. So no matter which activity does what, when you refresh data on load or resume of your recycler view you will always get correct results.
Post a Comment for "Android - What Is The Best Practice For Passing Back Information From An Activity Without Onactivityresult()?"