0

I have two activities "A" and "B". In "A" I have a RecyclerView and an "add" btn. After I press "add" new "activity B" via "startActivityForResult" opens. I fill fields in "B" -> press save btn -> starts new "AsyncTask" in which I add new element to my database. enter image description here And the question is how to update RecyclerView data in "A activity" the right way?

I'm not providing code because I would like to hear the answer in general how to build my app architecture.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    read about `startActivityForResult` – Pavneet_Singh Nov 21 '17 at 10:47
  • Possible duplicate of [Android: how to make an activity return results to the activity which calls it?](https://stackoverflow.com/questions/14785806/android-how-to-make-an-activity-return-results-to-the-activity-which-calls-it) – Dima Kozhevin Nov 21 '17 at 10:54
  • Yes I know about startActivityForResult. The question is how to update data in "acitivity A" when i'll insert new data in db, I do it asynchronously so i can call onActivityResult earlier than asyncTask will finish it's job –  Nov 21 '17 at 11:08

2 Answers2

0

After activity B is closed and you get the result you must update the recyclerview adapter with the new data again and call notifyDatasetChanged()

Ismail Iqbal
  • 2,774
  • 1
  • 25
  • 46
  • Yes, but it's not 100% that my asyncTask wil finish it work when i'll call for update from A activity –  Nov 21 '17 at 11:05
  • you can use EventBus which is a powerful and easy implementation of publisher subscriber pattern to handle this. – Behzad Lotfi Nov 21 '17 at 11:46
0

In case you created your own subclass of ContentProvider you can call notifyChange in function insert in this way:

getContext().getContentResolver().notifyChange(uri, null);