Make back behaviour work better for manage repos/installed apps/search.

This commit is contained in:
Peter Serwylo 2017-03-15 08:37:51 +11:00
parent 43a9a9d229
commit 60a2ebe9fa
3 changed files with 33 additions and 8 deletions

View File

@ -301,7 +301,10 @@
android:name=".AppUpdateStatusService" android:name=".AppUpdateStatusService"
android:exported="false" /> android:exported="false" />
<activity android:name=".views.main.MainActivity"> <activity
android:name=".views.main.MainActivity"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustResize">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />

View File

@ -6,7 +6,7 @@ import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView; import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils; import android.text.TextUtils;
@ -14,6 +14,8 @@ import android.view.MenuItem;
import android.widget.Toast; import android.widget.Toast;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;
import org.fdroid.fdroid.AppDetails; import org.fdroid.fdroid.AppDetails;
import org.fdroid.fdroid.AppDetails2; import org.fdroid.fdroid.AppDetails2;
import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.FDroidApp;
@ -42,7 +44,7 @@ import org.fdroid.fdroid.views.swap.SwapWorkflowActivity;
* When switching from one screen to the next, we stay within this activity. The new screen will * When switching from one screen to the next, we stay within this activity. The new screen will
* get inflated (if required) * get inflated (if required)
*/ */
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener { public class MainActivity extends AppCompatActivity implements BottomNavigationViewEx.OnNavigationItemSelectedListener {
private static final String TAG = "MainActivity"; private static final String TAG = "MainActivity";
@ -52,14 +54,17 @@ public class MainActivity extends AppCompatActivity implements BottomNavigationV
private static final String ACTION_ADD_REPO = "org.fdroid.fdroid.MainActivity.ACTION_ADD_REPO"; private static final String ACTION_ADD_REPO = "org.fdroid.fdroid.MainActivity.ACTION_ADD_REPO";
private static final String STATE_SELECTED_MENU_ID = "selectedMenuId";
private static final int REQUEST_SWAP = 3; private static final int REQUEST_SWAP = 3;
private RecyclerView pager; private RecyclerView pager;
private MainViewAdapter adapter; private MainViewAdapter adapter;
private BottomNavigationView bottomNavigation; private BottomNavigationViewEx bottomNavigation;
private int selectedMenuId = R.id.whats_new;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
@ -71,15 +76,30 @@ public class MainActivity extends AppCompatActivity implements BottomNavigationV
pager.setLayoutManager(new NonScrollingHorizontalLayoutManager(this)); pager.setLayoutManager(new NonScrollingHorizontalLayoutManager(this));
pager.setAdapter(adapter); pager.setAdapter(adapter);
bottomNavigation = (BottomNavigationView) findViewById(R.id.bottom_navigation); bottomNavigation = (BottomNavigationViewEx) findViewById(R.id.bottom_navigation);
bottomNavigation.setOnNavigationItemSelectedListener(this); bottomNavigation.setOnNavigationItemSelectedListener(this);
if (savedInstanceState != null) {
selectedMenuId = savedInstanceState.getInt(STATE_SELECTED_MENU_ID, R.id.whats_new);
}
setSelectedMenuInNav();
initialRepoUpdateIfRequired(); initialRepoUpdateIfRequired();
Intent intent = getIntent(); Intent intent = getIntent();
handleSearchOrAppViewIntent(intent); handleSearchOrAppViewIntent(intent);
} }
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_SELECTED_MENU_ID, selectedMenuId);
super.onSaveInstanceState(outState);
}
private void setSelectedMenuInNav() {
bottomNavigation.setCurrentItem(adapter.adapterPositionFromItemId(selectedMenuId));
}
/** /**
* The first time the app is run, we will have an empty app list. To deal with this, we will * The first time the app is run, we will have an empty app list. To deal with this, we will
* attempt to update with the default repo. However, if we have tried this at least once, then * attempt to update with the default repo. However, if we have tried this at least once, then
@ -103,7 +123,8 @@ public class MainActivity extends AppCompatActivity implements BottomNavigationV
if (getIntent().hasExtra(EXTRA_VIEW_UPDATES)) { if (getIntent().hasExtra(EXTRA_VIEW_UPDATES)) {
getIntent().removeExtra(EXTRA_VIEW_UPDATES); getIntent().removeExtra(EXTRA_VIEW_UPDATES);
pager.scrollToPosition(adapter.adapterPositionFromItemId(R.id.updates)); pager.scrollToPosition(adapter.adapterPositionFromItemId(R.id.updates));
bottomNavigation.findViewById(R.id.updates).performClick(); selectedMenuId = R.id.updates;
setSelectedMenuInNav();
} }
// AppDetails 2 and RepoDetailsActivity set different NFC actions, so reset here // AppDetails 2 and RepoDetailsActivity set different NFC actions, so reset here
@ -131,6 +152,7 @@ public class MainActivity extends AppCompatActivity implements BottomNavigationV
@Override @Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) { public boolean onNavigationItemSelected(@NonNull MenuItem item) {
pager.scrollToPosition(((MainViewAdapter) pager.getAdapter()).adapterPositionFromItemId(item.getItemId())); pager.scrollToPosition(((MainViewAdapter) pager.getAdapter()).adapterPositionFromItemId(item.getItemId()));
selectedMenuId = item.getItemId();
return true; return true;
} }

View File

@ -6,7 +6,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<android.support.design.widget.BottomNavigationView <com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="@+id/bottom_navigation" android:id="@+id/bottom_navigation"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"