android_calculator-47: Remember last used tab in vars, functions etc

This commit is contained in:
Sergey Solovyev 2012-01-09 19:40:53 +04:00
parent 2a4984f0ec
commit 96038c62d8
6 changed files with 126 additions and 4 deletions

View File

@ -11,6 +11,7 @@ import android.widget.TabHost;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.R;
import org.solovyev.android.view.LastTabSaver;
/**
* User: serso
@ -19,6 +20,9 @@ import org.solovyev.android.calculator.R;
*/
public class CalculatorAboutTabActivity extends TabActivity {
@Nullable
private LastTabSaver lastTabSaver;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -30,7 +34,17 @@ public class CalculatorAboutTabActivity extends TabActivity {
AndroidUtils.addTab(this, tabHost, "about", R.string.c_about, CalculatorAboutActivity.class);
AndroidUtils.addTab(this, tabHost, "release_notes", R.string.c_release_notes, CalculatorReleaseNotesActivity.class);
tabHost.setCurrentTab(0);
lastTabSaver = new LastTabSaver(this, "about");
AndroidUtils.centerAndWrapTabsFor(tabHost);
}
@Override
protected void onDestroy() {
if ( this.lastTabSaver != null ) {
this.lastTabSaver.destroy();
}
super.onDestroy();
}
}

View File

@ -15,6 +15,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.R;
import org.solovyev.android.view.LastTabSaver;
/**
* User: serso
@ -23,6 +24,9 @@ import org.solovyev.android.calculator.R;
*/
public class CalculatorHelpTabActivity extends TabActivity {
@Nullable
private LastTabSaver lastTabSaver;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -35,7 +39,7 @@ public class CalculatorHelpTabActivity extends TabActivity {
createTab(tabHost, "hints", R.string.c_hints, HelpHintsActivity.class);
createTab(tabHost, "screens", R.string.c_screens, HelpScreensActivity.class);
tabHost.setCurrentTab(0);
this.lastTabSaver = new LastTabSaver(this, "faq");
AndroidUtils.centerAndWrapTabsFor(tabHost);
}
@ -54,4 +58,13 @@ public class CalculatorHelpTabActivity extends TabActivity {
tabHost.addTab(spec);
}
@Override
protected void onDestroy() {
if (this.lastTabSaver != null) {
this.lastTabSaver.destroy();
}
super.onDestroy();
}
}

View File

@ -12,6 +12,7 @@ import android.widget.TabHost;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.R;
import org.solovyev.android.view.LastTabSaver;
/**
* User: serso
@ -20,6 +21,9 @@ import org.solovyev.android.calculator.R;
*/
public class CalculatorHistoryTabActivity extends TabActivity {
@Nullable
private LastTabSaver lastTabSaver;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -31,8 +35,16 @@ public class CalculatorHistoryTabActivity extends TabActivity {
AndroidUtils.addTab(this, tabHost, "saved_history", R.string.c_saved_history, SavedHistoryActivityTab.class);
AndroidUtils.addTab(this, tabHost, "history", R.string.c_history, HistoryActivityTab.class);
tabHost.setCurrentTab(0);
this.lastTabSaver = new LastTabSaver(this, "saved_history");
AndroidUtils.centerAndWrapTabsFor(tabHost);
}
@Override
protected void onDestroy() {
if ( this.lastTabSaver != null ) {
this.lastTabSaver.destroy();
}
super.onDestroy();
}
}

View File

@ -13,6 +13,7 @@ import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry;
import org.solovyev.android.view.LastTabSaver;
/**
* User: serso
@ -21,6 +22,9 @@ import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry;
*/
public class CalculatorFunctionsTabActivity extends TabActivity {
@Nullable
private LastTabSaver lastTabSaver;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -33,7 +37,16 @@ public class CalculatorFunctionsTabActivity extends TabActivity {
AbstractMathEntityListActivity.createTab(this, tabHost, category.name(), category.name(), category.getCaptionId(), CalculatorFunctionsActivity.class, null);
}
this.lastTabSaver = new LastTabSaver(this, AndroidFunctionsMathRegistry.Category.common.name());
AndroidUtils.centerAndWrapTabsFor(tabHost);
}
@Override
protected void onDestroy() {
if (lastTabSaver != null) {
this.lastTabSaver.destroy();
}
super.onDestroy();
}
}

View File

@ -13,6 +13,7 @@ import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.VarCategory;
import org.solovyev.android.view.LastTabSaver;
/**
* User: serso
@ -21,6 +22,9 @@ import org.solovyev.android.calculator.model.VarCategory;
*/
public class CalculatorVarsTabActivity extends TabActivity {
@Nullable
private LastTabSaver lastTabSaver;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -37,8 +41,16 @@ public class CalculatorVarsTabActivity extends TabActivity {
}
}
tabHost.setCurrentTab(0);
this.lastTabSaver = new LastTabSaver(this, VarCategory.my.name());
AndroidUtils.centerAndWrapTabsFor(tabHost);
}
@Override
protected void onDestroy() {
if (lastTabSaver != null) {
lastTabSaver.destroy();
}
super.onDestroy();
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2009-2012. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.view;
import android.app.TabActivity;
import android.preference.PreferenceManager;
import android.widget.TabHost;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.view.prefs.StringPreference;
/**
* User: serso
* Date: 1/9/12
* Time: 6:17 PM
*/
public class LastTabSaver implements TabHost.OnTabChangeListener {
private static final String LAST_OPENED_TAB_P_KEY = "last_opened_tab_";
@NotNull
private final StringPreference<String> preference;
@NotNull
private final TabActivity tabActivity;
public LastTabSaver(@NotNull TabActivity tabActivity, @NotNull String defaultTabId) {
this.tabActivity = tabActivity;
this.preference = StringPreference.newInstance(getPreferenceKey(), defaultTabId);
final TabHost tabHost = tabActivity.getTabHost();
tabHost.setCurrentTabByTag(this.getLastOpenedTabId());
tabHost.setOnTabChangedListener(this);
}
public void destroy() {
final TabHost tabHost = tabActivity.getTabHost();
tabHost.setOnTabChangedListener(null);
}
@Override
public void onTabChanged(String tabId) {
preference.putPreference(PreferenceManager.getDefaultSharedPreferences(tabActivity), tabId);
}
@NotNull
public String getLastOpenedTabId() {
return preference.getPreference(PreferenceManager.getDefaultSharedPreferences(tabActivity));
}
@NotNull
private String getPreferenceKey() {
return LAST_OPENED_TAB_P_KEY + tabActivity.getClass().getName();
}
}