问题
I have a rather general question. Assuming I have a RecyclerView in some kind of a MainActivity. The content of this RecyclerView is being updated in multiple places in other activities.
For example there could be the possibility to make new entries by starting a new Activity and saving it there.In that case I would intuitively start that activity with startActivityForResult() and receive the changes in the onActivityResult() method in the MainActivity.
But lets say deeper inside the application, there is the possibility to delete all entries. Now how do we notify the MainActivity about this change in the dataset? I thought about setting a flag of some kind and clearing it after the content has been updated in the MainActivity. But somehow using global variables does not really follow the principle of proper encapsulation, does it?
Sorry for this vague question, but I find it quite hard to properly handle information flow in Android in a elegant manner, so here we are.
回答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).
回答2:
You can use Handler to pass the Message in Activity and then You have to update RecyclerView. Like,
1) In Activity.
public static Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if(msg.what == 1223){
//update RecyclerView
}
return false;
}
});
2) pass message When you want to update RecyclerView
Message msg = new Message();
msg.what = 1223;
Activity1.mHandler.sendMessage(msg);
回答3:
You can use EventBus to handle it.
Define a class for your event
public static class MyEvent {
int event;
/* define your fields */
}
And prepare your subscriber in main activity
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMyEvent(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
回答4:
If you were using RxJava2, RxAndroid. Then you could try this.
Create a Bus:
public final class RxBus { private static final BehaviorSubject<Object> behaviorSubject = BehaviorSubject.create(); public static BehaviorSubject<Object> getSubject() { return behaviorSubject; } }In your WaitingActivity where you want to receive data(where you want not to use
onActivityResultin your case)Disposable disposable = RxBus.getSubject(). subscribeWith(new DisposableObserver<Object>() { @Override public void onNext(Object o) { if (o instanceof DataObject) { //((DataObject) o).getValue(); } } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); });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
@Override protected void onDestroy() { super.onDestroy(); disposable.dispose(); }
回答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.
来源:https://stackoverflow.com/questions/51221584/android-what-is-the-best-practice-for-passing-back-information-from-an-activit