simplify MainActivity and fix backstack issues

This commit is contained in:
Vincent Breitmoser 2015-05-17 01:41:51 +02:00
parent 064c9d461f
commit cd0d84d10d

View File

@ -44,12 +44,7 @@ import org.sufficientlysecure.keychain.util.Preferences;
public class MainActivity extends AppCompatActivity implements FabContainer { public class MainActivity extends AppCompatActivity implements FabContainer {
public Drawer.Result result; public Drawer.Result mDrawerResult;
private KeyListFragment mKeyListFragment ;
private AppsListFragment mAppsListFragment;
private EncryptDecryptOverviewFragment mEncryptDecryptOverviewFragment;
private Fragment mLastUsedFragment;
private Toolbar mToolbar; private Toolbar mToolbar;
@Override @Override
@ -57,25 +52,21 @@ public class MainActivity extends AppCompatActivity implements FabContainer {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity); setContentView(R.layout.main_activity);
//initialize FragmentLayout with KeyListFragment at first
Fragment mainFragment = new KeyListFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.main_fragment_container, mainFragment);
transaction.commit();
mToolbar = (Toolbar) findViewById(R.id.toolbar); mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setTitle(R.string.app_name); mToolbar.setTitle(R.string.app_name);
setSupportActionBar(mToolbar); setSupportActionBar(mToolbar);
result = new Drawer() mDrawerResult = new Drawer()
.withActivity(this) .withActivity(this)
.withHeader(R.layout.main_drawer_header) .withHeader(R.layout.main_drawer_header)
.withToolbar(mToolbar) .withToolbar(mToolbar)
.addDrawerItems( .addDrawerItems(
new PrimaryDrawerItem().withName(R.string.nav_keys).withIcon(CommunityMaterial.Icon.cmd_key).withIdentifier(1).withCheckable(false), new PrimaryDrawerItem().withName(R.string.nav_keys).withIcon(CommunityMaterial.Icon.cmd_key)
new PrimaryDrawerItem().withName(R.string.nav_encrypt_decrypt).withIcon(FontAwesome.Icon.faw_lock).withIdentifier(2).withCheckable(false), .withIdentifier(1).withCheckable(false),
new PrimaryDrawerItem().withName(R.string.title_api_registered_apps).withIcon(CommunityMaterial.Icon.cmd_apps).withIdentifier(3).withCheckable(false) new PrimaryDrawerItem().withName(R.string.nav_encrypt_decrypt).withIcon(FontAwesome.Icon.faw_lock)
.withIdentifier(2).withCheckable(false),
new PrimaryDrawerItem().withName(R.string.title_api_registered_apps).withIcon(CommunityMaterial.Icon.cmd_apps)
.withIdentifier(3).withCheckable(false)
) )
.addStickyDrawerItems( .addStickyDrawerItems(
// display and stick on bottom of drawer // display and stick on bottom of drawer
@ -130,76 +121,58 @@ public class MainActivity extends AppCompatActivity implements FabContainer {
OperationResult result = data.getParcelableExtra(OperationResult.EXTRA_RESULT); OperationResult result = data.getParcelableExtra(OperationResult.EXTRA_RESULT);
result.createNotify(this).show(); result.createNotify(this).show();
} }
}
private void clearFragments() { if (savedInstanceState == null) {
mKeyListFragment = null; // initialize FragmentLayout with KeyListFragment at first
mAppsListFragment = null; onKeysSelected();
mEncryptDecryptOverviewFragment = null; }
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
private void setFragment(Fragment fragment) {
setFragment(fragment, true);
} }
private void setFragment(Fragment fragment, boolean addToBackStack) { private void setFragment(Fragment fragment, boolean addToBackStack) {
this.mLastUsedFragment = fragment;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.main_fragment_container, fragment); ft.replace(R.id.main_fragment_container, fragment);
if (addToBackStack) { if (addToBackStack) {
ft.addToBackStack(null); ft.addToBackStack(null);
} }
ft.commit(); ft.commit();
} }
private boolean onKeysSelected() { private void onKeysSelected() {
mToolbar.setTitle(R.string.app_name); mToolbar.setTitle(R.string.app_name);
clearFragments(); Fragment frag = new KeyListFragment();
setFragment(frag, false);
if (mKeyListFragment == null) {
mKeyListFragment = new KeyListFragment();
}
setFragment(mKeyListFragment, false);
return true;
} }
private boolean onEnDecryptSelected() { private void onEnDecryptSelected() {
mToolbar.setTitle(R.string.nav_encrypt_decrypt); mToolbar.setTitle(R.string.nav_encrypt_decrypt);
clearFragments(); Fragment frag = new EncryptDecryptOverviewFragment();
if (mEncryptDecryptOverviewFragment == null) { setFragment(frag, true);
mEncryptDecryptOverviewFragment = new EncryptDecryptOverviewFragment();
}
setFragment(mEncryptDecryptOverviewFragment);
return true;
} }
private boolean onAppsSelected() { private void onAppsSelected() {
mToolbar.setTitle(R.string.nav_apps); mToolbar.setTitle(R.string.nav_apps);
clearFragments(); Fragment frag = new AppsListFragment();
if (mAppsListFragment == null) { setFragment(frag, true);
mAppsListFragment = new AppsListFragment();
}
setFragment(mAppsListFragment);
return true;
} }
@Override @Override
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
//add the values which need to be saved from the drawer to the bundle // add the values which need to be saved from the drawer to the bundle
outState = result.saveInstanceState(outState); outState = mDrawerResult.saveInstanceState(outState);
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }
@Override @Override
public void onBackPressed(){ public void onBackPressed() {
//handle the back press :D close the drawer first and if the drawer is closed close the activity // close the drawer first and if the drawer is closed do regular backstack handling
if (result != null && result.isDrawerOpen()) { if (mDrawerResult != null && mDrawerResult.isDrawerOpen()) {
result.closeDrawer(); mDrawerResult.closeDrawer();
} else { } else {
super.onBackPressed(); super.onBackPressed();
} }