19

I want to save/restore, in changing orientation, a List<Contact> instance for a ListView; Contact is the item to show in a custom array adapter.

I'm saving the List underlying the adapter in onSaveInstanceState(Bundle savingInstanceState) activity method:

savingInstanceState.putSerializable(""+R.id.contactList, (Serializable)adapter.contacts());

Then in onRestoreInstanceState(Bundle savedInstanceState) I restore and set the adapter:

    List<Contact> contacts =  
        (List<Contact>)savedInstanceState.getSerializable(""+R.id.contactList);
    ContactListAdapter adapter = 
        new ContactListAdapter((Context)this, android.R.layout.simple_list_item_1, contacts);
    contactList.setAdapter(adapter);
    adapter.notifyDataSetChanged();

Is it the right way to do it?

Do you have a better/smarter solution?

Thank you

Giorgio Vespucci
  • 1,586
  • 3
  • 18
  • 30

0 Answers0