Preventing activity while saving state when user selects back button

I have an Activity with a list box related to reading ListAdapter data into ArrayList from database. Everything is fine when the data is loaded first. As long as the activity is open and the list is displayed, it is possible that the data in the database will be updated by the service, but the list does not reflect the change since the ArrayList is unaware of the change. If the Activity is no longer in the foreground, as if the user went to the home screen and then returned to the foreground, I would like the Activity not to display what was done before, but instead of reloading the data using a ListAdapter. with which the view is associated. I think something needs to be called finish (), but I'm not sure what. This is what I have in Activity.

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpViews();
app = (MyApplication) getApplication();
adapter = new MyListAdapter(this, app.getMyEvents());
setListAdapter(adapter);
}
@Override
protected void onResume() {
super.onResume();
adapter.forceReload();
}

      

+2


a source to share


1 answer


I have an Activity with a list that is data bound to read ListAdapter into ArrayList from database.

Why not use it CursorAdapter

instead of reading everything in ArrayAdapter

?



As long as the activity is open and the list is displayed and probably the data in the database will be updated by the service but this list does not reflect the change, because the ArrayList will not be aware of the changes.

If you have used CursorAdapter

, you can simply call requery()

in Cursor

and your list will be updated with the latest database contents. For example, you could do this in onStart()

, and it will fire whenever the user returns to activity - from the home screen, from an incoming phone call they took, etc.

+2


a source







All Articles