Gratzi
Gratzi

Reputation: 4703

Updating the UI in onResume

Is it ok to update the UI of the activity in the onResume method? For example, to update the cursor adapter of a listview with a new cursor.

In my application I have an activity A and another one B. The UI, for both activities contains listviews with cursor adapters that depend on a common string list. I have buttons to navigate from A to B and also from B to A. The problem is, that if I go from A to B, and then, in the B activity I change the string list data and by that the UI data configuration, if I press BACK, I will go to A again, but the listview will not be updated according to the changes made in B.

Is using the onResume method in this manner a good practice, or is there a better solution?

Upvotes: 1

Views: 3631

Answers (4)

user4232
user4232

Reputation: 592

Use clear() for arraylist and then fill arraylist again,

 arraylist.clear();
 arraylist.add("your arraylit items ");
 setListAdapter(adapter);
 adapter.notifyDataSetChanged();

Upvotes: 0

kjdion84
kjdion84

Reputation: 10064

I just created a function for this.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_log);
    dbStuff();
}

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

public dbStuff(){    
    LogDatabaseHelper logDatabaseHelper = new LogDatabaseHelper(this);
    SQLiteDatabase database = logDatabaseHelper.getWritableDatabase();
    Cursor cursor = database.rawQuery("SELECT * FROM " + LogDatabaseHelper.TABLE_NAME + " ORDER BY id DESC", null);

    // add/manipulate views using the cursor
}

I'm using the cursor to create views programatically, dynamically inside a LinearLayout.

If you wanna do that, I also use this to make sure the LinearLayout is cleared when the data gets refreshed:

final LinearLayout dbContainer = findViewById(R.id.dbLayout);

if (dbContainer.getChildCount() > 0) {
    dbContainer.removeAllViews();
}

// add the views here

Upvotes: 0

Francesco Laurita
Francesco Laurita

Reputation: 23552

I think the right method is to use startActivityForResult

For instance you can start the activity B from A by using:

.....
    Intent intent = new Intent(this, B.class);
    startActivityForResult(intent);
....


// Listen for results from B.
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    // See which child activity is calling us back.
    switch (resultCode) {
        case 0://Zero ie means data has been changed into the B activity
           //Update the cursor
        default://No changes from B so do nothing
            break;
    }
}

Upvotes: 0

Jems
Jems

Reputation: 11620

Rather than using a new cursor, would simply calling notifyDataSetChanged() work? If it seems likely that every time you come back to the Activity it will need updating, then you must update the UI in onResume or onRestart/onStart.

Upvotes: 1

Related Questions