Question

Here's the situation: I have an app with a Navigation Drawer. There are several fragments, one which is a dashboard to quickly show relevant information, and another which shows a list of data saved by the user. On the dashboard is a View which shows a specific item from the second fragment, and when either this view or any item from the second fragment is clicked, I go to a new Activity to edit the selected item.

The problem is getting the edits to be reflected in the fragments when returning from the edit Activity. Navigating back from to the ListFragment is easy, I just repopulate the list adapter, but for the single View on the dashboard, I cannot get it to refresh properly. By overriding onResume() and calling invalidate() on the View, I can get it to refresh only when switching between fragments from the Navigation Drawer, but returning from the edit Activity does not refresh it, even though I have verified that onResume() is being called for this fragment.

I have tried calling invalidate(), postInvalidate() on this view and its layout, creating a new instance of the view, and still it will only refresh on changing fragments with the Drawer. What could be preventing invalidate() from redrawing my view when returning from an activity?

EDIT The view in question is a custom view containing two TextViews and a button which toggles a notification. The data to be changed should just be the text of these TextViews, whatever the user changes them to. The view is pulling data from a SQLite database, which is updated just before the user returns from the edit activity, so by the time the dashboard is reloaded, the data the view is gathering should reflect the updates, so the fact that it does not makes me believe that this view has not yet been refreshed, given that the docs says, "If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future. ". The following is my code which works in changing fragments with the drawer, but not in returning from the activity, though it is still called in the latter case:

@Override
public void onResume() {
    super.onResume();
    notify.invalidate(); //the view in question 
}
Était-ce utile?

La solution

By calling invalidate() the View is only redrawn. That has no effect on the data displayed by the View. If you want to update the data displayed in your custom view you have to set it again to your custom view in onResume like I do in this example with a TextView:

@Override
public void onResume() {
    super.onResume();

    this.textView.setText(textToDisplay);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top