I have a DrawerLayout on the front and NavigationView at the backend code.
I have 3 Activity, MainActivity ( which contain DrawerLayout ), TrainActivity, SettingActivity.
When i want to navigate from Main -> Train, i just start the intent and not to finish the MainActivity ( because MainActivity contain googlemap ). I save the MainActivity into backstack.
After navigating Main -> Train, then in TrainActivity i want to navigate back to MainActivity ( onBackPressed or whatever the method is ).
The problem is the NavigationView on MainActivity cannot start the intent when i choose Train or SettingActivity.
The navigate scenario Main - > Train -> Main -> Setting / Train, but what i've got is Main - > Train - > ( Back to Main ) -> NavigationView cannot be selected again ( checklisted menu on NavigationView ).
Here is the code :
private void setupDrawerContent(NavigationView navigationView) {
initNavigationProfile();
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
final Context context = getBaseContext();
final Handler handler = new Handler();
switch (menuItem.getItemId()) {
case R.id.map_home:
showLoadingBar();
final Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
handler.postDelayed(new Runnable() {
@Override
public void run() {
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
handler.removeCallbacks(this);
hideLoadingBar();
finish();
}
}, 300L);
break;
case R.id.train_schedule:
showLoadingBar();
final Intent trainIntent = new Intent(getApplicationContext(), TrainScheduleActivity.class);
handler.postDelayed(new Runnable() {
@Override
public void run() {
trainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(trainIntent);
handler.removeCallbacks(this);
hideLoadingBar();
finish();
}
}, 300L);
break;
Here the code onbackPressed();
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
I don't have any idea with what makes the NavigationView couldn't select any item.