Clean up main activity code

No need to catch IllegalStateException as we display the fragment from
onCreate() without delay.
This commit is contained in:
tonymanou 2017-01-12 01:32:16 +01:00 committed by topjohnwu
parent 4dbacd79ae
commit e81bc4f044

View File

@ -10,7 +10,6 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView; import android.support.design.widget.NavigationView;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
@ -44,7 +43,6 @@ public class MainActivity extends AppCompatActivity
@BindView(R.id.drawer_layout) DrawerLayout drawer; @BindView(R.id.drawer_layout) DrawerLayout drawer;
@BindView(R.id.nav_view) public NavigationView navigationView; @BindView(R.id.nav_view) public NavigationView navigationView;
@IdRes
private int mSelectedId = R.id.status; private int mSelectedId = R.id.status;
@Override @Override
@ -82,13 +80,11 @@ public class MainActivity extends AppCompatActivity
drawer.addDrawerListener(toggle); drawer.addDrawerListener(toggle);
toggle.syncState(); toggle.syncState();
//noinspection ResourceType
mSelectedId = savedInstanceState == null ? mSelectedId : savedInstanceState.getInt(SELECTED_ITEM_ID);
navigationView.setCheckedItem(mSelectedId);
if (savedInstanceState == null) { if (savedInstanceState == null) {
mDrawerHandler.removeCallbacksAndMessages(null); navigate(mSelectedId, true);
mDrawerHandler.postDelayed(() -> navigate(mSelectedId), 250); navigationView.setCheckedItem(mSelectedId);
} else {
mSelectedId = savedInstanceState.getInt(SELECTED_ITEM_ID);
} }
navigationView.setNavigationItemSelectedListener(this); navigationView.setNavigationItemSelectedListener(this);
@ -136,7 +132,7 @@ public class MainActivity extends AppCompatActivity
public boolean onNavigationItemSelected(@NonNull final MenuItem menuItem) { public boolean onNavigationItemSelected(@NonNull final MenuItem menuItem) {
mSelectedId = menuItem.getItemId(); mSelectedId = menuItem.getItemId();
mDrawerHandler.removeCallbacksAndMessages(null); mDrawerHandler.removeCallbacksAndMessages(null);
mDrawerHandler.postDelayed(() -> navigate(menuItem.getItemId()), 250); mDrawerHandler.postDelayed(() -> navigate(menuItem.getItemId(), false), 250);
drawer.closeDrawer(GravityCompat.START); drawer.closeDrawer(GravityCompat.START);
return true; return true;
} }
@ -164,48 +160,40 @@ public class MainActivity extends AppCompatActivity
menu.findItem(R.id.install).setVisible(Shell.rootAccess()); menu.findItem(R.id.install).setVisible(Shell.rootAccess());
} }
public void navigate(final int itemId) { public void navigate(int itemId, boolean now) {
Fragment navFragment = null;
String tag = "";
switch (itemId) { switch (itemId) {
case R.id.status: case R.id.status:
tag = "status"; displayFragment(new StatusFragment(), "status", now);
navFragment = new StatusFragment();
break; break;
case R.id.install: case R.id.install:
tag = "install"; displayFragment(new InstallFragment(), "install", now);
navFragment = new InstallFragment();
break; break;
case R.id.modules: case R.id.modules:
tag = "modules"; displayFragment(new ModulesFragment(), "modules", now);
navFragment = new ModulesFragment();
break; break;
case R.id.downloads: case R.id.downloads:
tag = "downloads"; displayFragment(new ReposFragment(), "downloads", now);
navFragment = new ReposFragment();
break; break;
case R.id.magiskhide: case R.id.magiskhide:
tag = "magiskhide"; displayFragment(new MagiskHideFragment(), "magiskhide", now);
navFragment = new MagiskHideFragment();
break; break;
case R.id.log: case R.id.log:
tag = "log"; displayFragment(new LogFragment(), "log", now);
navFragment = new LogFragment();
break; break;
case R.id.settings: case R.id.settings:
startActivity(new Intent(this, SettingsActivity.class)); startActivity(new Intent(this, SettingsActivity.class));
break; break;
case R.id.app_about: case R.id.app_about:
startActivity(new Intent(this, AboutActivity.class)); startActivity(new Intent(this, AboutActivity.class));
return; break;
}
if (navFragment != null) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
try {
transaction.replace(R.id.content_frame, navFragment, tag).commit();
} catch (IllegalStateException ignored) {}
} }
} }
private void displayFragment(@NonNull Fragment navFragment, String tag, boolean now) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
if (!now) {
transaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
}
transaction.replace(R.id.content_frame, navFragment, tag).commit();
}
} }