0

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.

ByanJati
  • 83
  • 1
  • 11
  • I don't think you should be using Runnable to start another activity. Just call startActivity after new Intent – user3641702 Nov 25 '15 at 12:24
  • i have been add the onBackPressed code on another activity – ByanJati Nov 25 '15 at 12:25
  • @user3641702 hmm . . . i think it was my additional code to make an UX for the transition. But does it relate with navigationview problem ? – ByanJati Nov 25 '15 at 12:27
  • Your onBackPressed seems ok. The runnable could be interfering with the way the activities are launched. If you open your main activity, do you experience any strange behaviour with the navigation drawer? Or do the problems occur when going from one activity to the main activity? – user3641702 Nov 25 '15 at 14:35
  • the problems occur when going to main activity to another activity after come back from another activity, it seems quite complex to say it. But heres the scenario : Main -> Train -> Back to Main -> Error Occured ( cant select and inflate new activity ) – ByanJati Nov 25 '15 at 19:08

0 Answers0