Refresh Recyclerview from another Activity Refresh Recyclerview from another Activity sqlite sqlite

Refresh Recyclerview from another Activity


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:

@Overridepublic 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:

@Overrideprotected 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    }}


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.


Try Like this,

Intent intent = new Intent(Context, YourClass);startActivityForResult(intent, ADD_ITEM);protected void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (resultCode == Activity.RESULT_OK) {        if (requestCode == ADD_ITEM){            adapterItem.addItem(MyApplication.getWritableDatabase().getAllItems());            listProjectsView.scrollToPosition(0);        }    }       }

hope it will work for you