Improved MainActivity.switchFragment. Close #34

This commit is contained in:
PeratX 2017-08-23 09:33:48 +08:00
parent 792891a53b
commit 872ade01d3
2 changed files with 29 additions and 22 deletions

View File

@ -41,8 +41,7 @@ android {
} }
dependencies { dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1' compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1' compile 'com.android.support:support-v4:25.3.1'

View File

@ -25,6 +25,7 @@ import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R; import org.itxtech.daedalus.R;
import org.itxtech.daedalus.fragment.*; import org.itxtech.daedalus.fragment.*;
import org.itxtech.daedalus.service.DaedalusVpnService; import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.Logger;
import org.itxtech.daedalus.util.server.DNSServerHelper; import org.itxtech.daedalus.util.server.DNSServerHelper;
/** /**
@ -92,10 +93,17 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
Log.d(TAG, "onCreate"); Log.d(TAG, "onCreate");
} }
private void switchFragment(ToolbarFragment fragment) { private void switchFragment(Class fragmentClass) {
FragmentManager fm = getFragmentManager(); if (currentFragment == null || fragmentClass != currentFragment.getClass()) {
fm.beginTransaction().replace(R.id.id_content, fragment).commit(); try {
currentFragment = fragment; ToolbarFragment fragment = (ToolbarFragment) fragmentClass.newInstance();
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.id_content, fragment).commit();
currentFragment = fragment;
} catch (Throwable e){
Logger.logException(e);
}
}
} }
@Override @Override
@ -104,7 +112,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
if (drawer.isDrawerOpen(GravityCompat.START)) { if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START); drawer.closeDrawer(GravityCompat.START);
} else if (!(currentFragment instanceof HomeFragment)) { } else if (!(currentFragment instanceof HomeFragment)) {
switchFragment(new HomeFragment()); switchFragment(HomeFragment.class);
} else { } else {
super.onBackPressed(); super.onBackPressed();
} }
@ -200,29 +208,29 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
int fragment = intent.getIntExtra(LAUNCH_FRAGMENT, FRAGMENT_NONE); int fragment = intent.getIntExtra(LAUNCH_FRAGMENT, FRAGMENT_NONE);
switch (fragment) { switch (fragment) {
case FRAGMENT_ABOUT: case FRAGMENT_ABOUT:
switchFragment(new AboutFragment()); switchFragment(AboutFragment.class);
break; break;
case FRAGMENT_DNS_SERVERS: case FRAGMENT_DNS_SERVERS:
switchFragment(new DNSServersFragment()); switchFragment(DNSServersFragment.class);
break; break;
case FRAGMENT_DNS_TEST: case FRAGMENT_DNS_TEST:
switchFragment(new DNSTestFragment()); switchFragment(DNSTestFragment.class);
break; break;
case FRAGMENT_HOME: case FRAGMENT_HOME:
switchFragment(new HomeFragment()); switchFragment(HomeFragment.class);
break; break;
case FRAGMENT_RULES: case FRAGMENT_RULES:
switchFragment(new RulesFragment()); switchFragment(RulesFragment.class);
break; break;
case FRAGMENT_SETTINGS: case FRAGMENT_SETTINGS:
switchFragment(new SettingsFragment()); switchFragment(SettingsFragment.class);
break; break;
case FRAGMENT_LOG: case FRAGMENT_LOG:
switchFragment(new LogFragment()); switchFragment(LogFragment.class);
break; break;
} }
if (currentFragment == null) { if (currentFragment == null) {
switchFragment(new HomeFragment()); switchFragment(HomeFragment.class);
} }
} }
@ -233,28 +241,28 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
switch (id) { switch (id) {
case R.id.nav_about: case R.id.nav_about:
switchFragment(new AboutFragment()); switchFragment(AboutFragment.class);
break; break;
case R.id.nav_dns_server: case R.id.nav_dns_server:
switchFragment(new DNSServersFragment()); switchFragment(DNSServersFragment.class);
break; break;
case R.id.nav_dns_test: case R.id.nav_dns_test:
switchFragment(new DNSTestFragment()); switchFragment(DNSTestFragment.class);
break; break;
case R.id.nav_github: case R.id.nav_github:
Daedalus.openUri("https://github.com/iTXTech/Daedalus"); Daedalus.openUri("https://github.com/iTXTech/Daedalus");
break; break;
case R.id.nav_home: case R.id.nav_home:
switchFragment(new HomeFragment()); switchFragment(HomeFragment.class);
break; break;
case R.id.nav_rules: case R.id.nav_rules:
switchFragment(new RulesFragment()); switchFragment(RulesFragment.class);
break; break;
case R.id.nav_settings: case R.id.nav_settings:
switchFragment(new SettingsFragment()); switchFragment(SettingsFragment.class);
break; break;
case R.id.nav_log: case R.id.nav_log:
switchFragment(new LogFragment()); switchFragment(LogFragment.class);
break; break;
} }