I have a FragmentActivity that shows a list of contacts.
Here is my onCreate method:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_human_list); if (findViewById(R.id.human_detail_container) != null) { // The detail container view will be present only in the // large-screen layouts (res/values-large and // res/values-sw600dp). If this view is present, then the // activity should be in two-pane mode. mTwoPane = true; // In two-pane mode, list items should be given the // 'activated' state when touched. ((HumanListFragment) getSupportFragmentManager() .findFragmentById(R.id.human_list)) .setActivateOnItemClick(true); } if (savedInstanceState == null || !savedInstanceState.getBoolean("displayed_contacts")) displayContacts(); }
My onSaveInstanceState :
@Override protected void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); savedInstanceState.putBoolean("displayed_contacts", true); }
And I'm not sure if this is relevant, but here is my displayContacts just in case:
private void displayContacts() { // Init variables String[] SelectColumns = new String[] { Contacts._ID, Contacts.DISPLAY_NAME_PRIMARY, Contacts.PHOTO_URI }; String rawContactID, displayName, phoneNumber; InputStream thumbnailPhoto; Cursor c, infoC; // Outer cursor (fetches all contact IDs) c = getContentResolver().query( Contacts.CONTENT_URI, SelectColumns, Contacts.HAS_PHONE_NUMBER + " = 1 ", null, Contacts.DISPLAY_NAME_PRIMARY); Log.v(getPackageName(), "Found " + (c != null ? c.getCount() : "0") + " contacts"); try { if (c.moveToFirst()) { do { // Columns rawContactID = c.getString(c.getColumnIndex(SelectColumns[0])); displayName = c.getString(c.getColumnIndex(SelectColumns[1])); String[] selectPhone = {CommonDataKinds.Phone.NUMBER}; thumbnailPhoto = openThumbnail(Long.valueOf(rawContactID)); infoC = getContentResolver().query( CommonDataKinds.Phone.CONTENT_URI, selectPhone, CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {rawContactID}, null ); infoC.moveToFirst(); phoneNumber = infoC.getString(0); // Adds items to ListView HumanContent.addItem(new HumanContent.HumanItem(rawContactID, displayName, phoneNumber != "n/a" ? phoneNumber : "", thumbnailPhoto)); Log.v(getPackageName(), "Cursor position: " + c.getPosition() + ", contact ID: " + rawContactID); infoC.close(); } while (c.moveToNext()); c.close(); } displayed_contacts = true; } catch (Exception e) { Log.e(getPackageName(), e.getMessage()); } }
Now here is what:
When I use the back key to exit the application, and then open it again with the icon; the list is recreated even if it is stored in memory: therefore, I get a double list of contacts in the same view.
savedInstanceState in this case is zero, so the if condition is met, but in fact the view already has my previous contact list. What gives? How can I avoid recreating the list? I already tried using instance variables instead, but to no avail.
I would also like to avoid re-creating the list 100% of the time - if I can reuse an existing view, amazing.