Handle saving and communicating selected tab between View/Edit activities.
EditContactActivity now accepts intents with raw_contacts instead of
contacts. In addition it reports back which raw_contact was selected on
exit.
ViewContactActivity now saves and restores which raw_contact was
currently being viewed, and uses the raw_contact reported back from
EditContactActivity. The overall effect is when moving from View->Edit
or Edit->View the selected tab doesn't change.
diff --git a/src/com/android/contacts/BaseContactCardActivity.java b/src/com/android/contacts/BaseContactCardActivity.java
index ef84e69..441aea1 100644
--- a/src/com/android/contacts/BaseContactCardActivity.java
+++ b/src/com/android/contacts/BaseContactCardActivity.java
@@ -72,6 +72,9 @@
protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1;
protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2;
+ protected static final String SELECTED_RAW_CONTACT_ID_KEY = "selectedRawContact";
+ protected long mSelectedRawContactId;
+
private static final int TOKEN_TABS = 0;
@Override
@@ -101,6 +104,19 @@
asyncSetupTabs();
}
+
+ @Override
+ protected void onRestoreInstanceState(Bundle savedInstanceState) {
+ super.onRestoreInstanceState(savedInstanceState);
+ mSelectedRawContactId = savedInstanceState.getLong(SELECTED_RAW_CONTACT_ID_KEY);
+ }
+
+ @Override
+ protected void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putLong(SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
+ }
+
private void asyncSetupTabs() {
long contactId = ContentUris.parseId(mUri);
mHandler.startQueryEntities(TOKEN_TABS, null,
@@ -108,7 +124,7 @@
}
/**
- * Return the contactId associated with the tab at an index.
+ * Return the RawContact id associated with the tab at an index.
*
* @param index The index of the tab in question.
* @return The contactId associated with the tab at the specified index.
@@ -117,6 +133,22 @@
return mTabRawContactIdMap.get(index);
}
+ /**
+ * Return the tab index associated with the RawContact id.
+ *
+ * @param index The index of the tab in question.
+ * @return The contactId associated with the tab at the specified index.
+ */
+ protected int getTabIndexForRawContactId(long rawContactId) {
+ int numTabs = mTabRawContactIdMap.size();
+ for (int i=0; i < numTabs; i++) {
+ if (mTabRawContactIdMap.get(i) == rawContactId) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
/** {@inheritDoc} */
public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
try{
@@ -170,7 +202,8 @@
ContactsSource.LEVEL_SUMMARY);
addTab(rawContactId, createTabIndicatorView(mTabWidget, source));
}
- mTabWidget.setCurrentTab(0);
+
+ selectInitialTab();
mTabWidget.setVisibility(View.VISIBLE);
mTabWidget.postInvalidate();
}
@@ -204,6 +237,18 @@
mTabWidget.removeAllTabs();
}
+ protected void selectInitialTab() {
+ int selectedTabIndex = -1;
+ if (mSelectedRawContactId > 0) {
+ selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
+ }
+ if (selectedTabIndex >= 0) {
+ mTabWidget.setCurrentTab(selectedTabIndex);
+ } else {
+ selectDefaultTab();
+ }
+ }
+
/**
* Makes the default tab selection. This is called after the tabs have been
* bound for the first time, and whenever a new intent is received. Override