How refresh the recycle-view from another activity when go back…

Lahiru Ariyasinghe
2 min readJan 14, 2019
Photo by Georgia de Lotz on Unsplash

I believe sometime you were suffering this problem when refresh a recycle-view adapter when you come back from another activity. Typically we are doing restart the activity if there have a change in second activity or nothing do if there have no any change. so I used this steps to solve that.actually Its not a new process, but It will helpful to you as a professional in android. otherwise If we going to restart activities when we come back at all times, It will cause to inefficiency of data usage and It can be cause to lack of performances in app. so we can overview that problem by using following three steps and you can customized as your own purposes.

Step 1

In your recycle-view adapter, you want to start the new activity as following,

Context context = v.getContext();
Intent intent = new Intent(context, Activity2.class);
intent.putExtra("id", "1");
intent.putExtra("description", "send description to 2nd activity");
((Activity) context).startActivityForResult(intent,requestcode:1);

Step 2

In your second activity, you want to set the onBackpressed() as the following,

@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("id","1");
intent.putExtra("description","sendback description from 2nd activity");
setResult(RESULT_OK, intent);
finish();

}

Step 3

Then, The most important step, here you want to override the onActivityResult() method within first activity in your activity like following,

public void onActivityResult(int requestCode, int resultCode, Intent data) {



if(requestCode == 1)
mydapter.onActivityResult(requestCode,1);

}

So you can extract data from the intent(data) or without any data you can refresh the adapter by calling to a method in adapter class. In here I called to onActivityResult() method which include in adapter class.

public  void onActivityResult(int requestCode, int resultCode) {

this.notifyDataSetChanged();


}

Finally you can refresh the adapter by notifyDataSetChanged() or you can do anything you want to do to the recycle-view as new changes.

Thank you. Hope this was helpful. Please ask any questions that you have about this or any concerns about android.

--

--