Refresh Recyclerview from another Activity
Solution 1
Pass the new item back to MainActivity, then deal with it yourself.
1.In MainActivity, use startActivityForResult
to start the second Activity, like this:
startActivityForResult(new Intent(this, SecondActivity.class), REQUEST_CODE);
REQUEST_CODE
is an int
.
2.In second Activity, override finish
like this:
@Override
public void finish() {
Intent returnIntent = new Intent();
returnIntent.putExtra("passed_item", itemYouJustCreated);
// setResult(RESULT_OK);
setResult(RESULT_OK, returnIntent); //By not passing the intent in the result, the calling activity will get null data.
super.finish();
}
3.In MainActivity, override onActivityResult
like this:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
YourItem passedItem = data.getExtras().get("passed_item");
// deal with the item yourself
}
}
Solution 2
You can create static recyclerview (let say mRecyclerview) in the activity(let say firstActivity) which displays your recyclerview . And when you add new item to your list in the other activity(let say secondActivity), you can just use
firstActivity.mRecyclerview.getAdapter().notifyDataSetChanged();
I use this way for a while and didn't encounter any performance drawback but test it with your own code for sure.
Related videos on Youtube
Jose M
I'm an android programmer and web designer. Nowadays i'm trying to learn as much as i can about Android and all the stuff to make good apps. I enjoy drawing and designing too. I love cats.
Updated on September 15, 2022Comments
-
Jose M over 1 year
I have my main Activity with a fragment, which has a Recyclerview retrieving all the data populated from the database. In this fragment I got a button which calls another activity to insert an object into the database.
The problem is when the Activity ends and returns to the Main Activity the Recyclerview doesn't show the new item I just created. I tried with this piece of code:
@Override public void onResume() { super.onResume(); adapterItem.addItem(MyApplication.getWritableDatabase().getAllItems()); listProjectsView.scrollToPosition(0); }
The function getAllItems retrieve all the data from sqlite.
On my adapterItem from my Recyclerview I got this:
public void addItem(ArrayList<Item> listItems) { this.listItems = listItems; notifyItemInserted(0); }
It works "fine". The main problem is when I switch to another application in progress in my mobile and I return to my developed app the onResume method switches on and show again my last insert, populating twice this item (and so on if I do it again).
Thanks
-
Simon almost 9 yearsI tried this once but i was not able to get the add animation to show for the recyclerview - i guess it adds it before the activity became fully visible. Do you know how I can add an item from another activity and have the addition be animated on the recyclerview when I navigate back to my mainactivity?
-
Htoo Aung Hlaing almost 8 yearsrecyclerview click event is in RecyclerView's adapter, not in MainActivity,so how can i get for that?
-
Lucas P. over 6 yearsThanks for accepting my edit Wesley and thank you for your answer, you greatly helped on the project I'm working on :)
-
Hossain Ehsani over 3 yearsits a valid method, but it will only work when the Adapter is updated in the second activity like this : firstActivity.mRecyclerview.setAdapter(List<data>);