Imagine there is a MainActivity in which there is a ListView with an ArrayAdapter. In the views that belong to the MainActivity I update the data directly; for example, in my PageAdapter class:
@Override
public void finishUpdate(View arg0) {
ViewPager vp = (ViewPager) arg0;
if (vp.getCurrentItem() != this.current_item){
this.current_item = vp.getCurrentItem();
ContentStatus status;
switch(vp.getCurrentItem()){
case(0):
status = ContentStatus.NOTINTERESTING;
break;
case(2):
status = ContentStatus.INTERESTING;
break;
default:
status = ContentStatus.ACTIVE;
}
MainActivity.content.setItemStatus(content.identifier, status);
final Activity act = (Activity) this.context;
ArrayAdapter adapter = (ArrayAdapter) ((ListView) act.findViewById(R.id.view_sequence)).getAdapter();
adapter.notifyDataSetChanged();
}
}
I need to reproduce this code in another activity which opens from the MainActivity and uses the same data, because if the user changes some data from the other activity and then closes it and returns to the MainActivity I must redraw MainActivity‘s views. As a variant, should I use Activity‘s onResume() method to update all views that haves bindings to data?
Short answer? Yes – if your activity’s view state might change every time the activity is paused and resumed, then it makes sense to update them all on a resume. This is more in line with the ‘android way’ if you will – two activities should not affect each other unless they are somehow working with the same data. If they are working with the same data, You might consider making your second activity a
Fragmentinstead; that way all operations related to that bit of data are tied to a single activity with multiple fragments.