save/restore selected tab when app gets killed

Implementation attempt number two.

* e2244d2afe
!1274
!1256
This commit is contained in:
Hans-Christoph Steiner
2023-09-06 10:00:12 +02:00
parent 049d9d8d3c
commit f43ef0becf
2 changed files with 40 additions and 4 deletions

View File

@@ -132,6 +132,7 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
// not shown in Settings
private static final String PREF_LAST_UPDATE_CHECK = "lastUpdateCheck";
private static final String PREF_BOTTOM_NAVIGATION_VIEW_NAME = "bottomNavigationViewName";
// these preferences are not listed in preferences.xml so the defaults are set here
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
@@ -337,6 +338,15 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
preferences.edit().putLong(PREF_LAST_UPDATE_CHECK, lastUpdateCheck).apply();
}
public String getBottomNavigationViewName() {
return preferences.getString(PREF_BOTTOM_NAVIGATION_VIEW_NAME,
org.fdroid.fdroid.views.main.MainActivity.EXTRA_VIEW_LATEST);
}
public void setBottomNavigationViewName(final String viewName) {
preferences.edit().putString(PREF_BOTTOM_NAVIGATION_VIEW_NAME, viewName).apply();
}
/**
* The first time the app has been run since fresh install or clearing all data.
*/

View File

@@ -80,8 +80,10 @@ public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
public static final String EXTRA_VIEW_UPDATES = "org.fdroid.fdroid.views.main.MainActivity.VIEW_UPDATES";
public static final String EXTRA_VIEW_LATEST = "org.fdroid.fdroid.views.main.MainActivity.VIEW_LATEST";
private static final String EXTRA_VIEW_CATEGORIES = "org.fdroid.fdroid.views.main.MainActivity.VIEW_CATEGORIES";
private static final String EXTRA_VIEW_NEARBY = "org.fdroid.fdroid.views.main.MainActivity.VIEW_NEARBY";
public static final String EXTRA_VIEW_UPDATES = "org.fdroid.fdroid.views.main.MainActivity.VIEW_UPDATES";
public static final String EXTRA_VIEW_SETTINGS = "org.fdroid.fdroid.views.main.MainActivity.VIEW_SETTINGS";
static final int REQUEST_LOCATION_PERMISSIONS = 0xEF0F;
@@ -122,13 +124,23 @@ public class MainActivity extends AppCompatActivity {
pager.setAdapter(adapter);
bottomNavigation = findViewById(R.id.bottom_navigation);
setSelectedMenuInNav(Preferences.get().getBottomNavigationViewName());
bottomNavigation.setOnNavigationItemSelectedListener(item -> {
pager.scrollToPosition(item.getOrder());
if (item.getItemId() == R.id.nearby) {
NearbyViewBinder.updateUsbOtg(MainActivity.this);
}
if (item.getItemId() == R.id.latest) {
Preferences.get().setBottomNavigationViewName(EXTRA_VIEW_LATEST);
} else if (item.getItemId() == R.id.categories) {
Preferences.get().setBottomNavigationViewName(EXTRA_VIEW_CATEGORIES);
} else if (item.getItemId() == R.id.nearby) {
Preferences.get().setBottomNavigationViewName(EXTRA_VIEW_NEARBY);
NearbyViewBinder.updateUsbOtg(MainActivity.this);
} else if (item.getItemId() == R.id.updates) {
Preferences.get().setBottomNavigationViewName(EXTRA_VIEW_UPDATES);
} else if (item.getItemId() == R.id.settings) {
Preferences.get().setBottomNavigationViewName(EXTRA_VIEW_SETTINGS);
}
return true;
});
@@ -157,6 +169,20 @@ public class MainActivity extends AppCompatActivity {
bottomNavigation.getMenu().getItem(position).setChecked(true);
}
private void setSelectedMenuInNav(final String viewName) {
if (EXTRA_VIEW_LATEST.equals(viewName)) {
setSelectedMenuInNav(R.id.latest);
} else if (EXTRA_VIEW_CATEGORIES.equals(viewName)) {
setSelectedMenuInNav(R.id.categories);
} else if (EXTRA_VIEW_NEARBY.equals(viewName)) {
setSelectedMenuInNav(R.id.nearby);
} else if (EXTRA_VIEW_UPDATES.equals(viewName)) {
setSelectedMenuInNav(R.id.updates);
} else if (EXTRA_VIEW_SETTINGS.equals(viewName)) {
setSelectedMenuInNav(R.id.settings);
}
}
private void initialRepoUpdateIfRequired() {
if (Preferences.get().isIndexNeverUpdated() && !UpdateService.isUpdating()) {
Utils.debugLog(TAG, "We haven't done an update yet. Forcing repo update.");