title: Navigation View Selected Menu Item Checked In Other Activity?
body: <p>i am Using Material Design Navigation View..i created Menu item And linked activity(Activity2.java) with an item(Starred)...and in that activity i extend my Main activity in which there is navigation view so that i can slide the navigation view from that activity too.. but when i slide the navigation view from the second activity the item is not checked and if i press the back button and go to the Main activity the previously checked menu item is shown...how to update the check item in other activity.please help</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      //Defining Variables
      private Toolbar toolbar;
      private NavigationView navigationView;
      private DrawerLayout drawerLayout;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // Initializing Toolbar and setting it as the actionbar
          toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          //Initializing NavigationView
          navigationView = (NavigationView) findViewById(R.id.navigation_view);
  
          //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
          navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
  
              // This method will trigger on item Click of navigation menu
              @Override
              public boolean onNavigationItemSelected(MenuItem menuItem) {
  
  
                  //Checking if the item is in checked state or not, if not make it in checked state
                  if(menuItem.isChecked()) menuItem.setChecked(false);
                  else menuItem.setChecked(true);
  
                  //Closing drawer on item click
                  drawerLayout.closeDrawers();
  
                  //Check to see which item was being clicked and perform appropriate action
                  switch (menuItem.getItemId()){
  
  
                      //Replacing the main content with ContentFragment Which is our Inbox View;
                      case R.id.inbox:
                          Toast.makeText(getApplicationContext(),"Inbox Selected",Toast.LENGTH_SHORT).show();
                          ContentFragment fragment = new ContentFragment();
                          android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                          fragmentTransaction.replace(R.id.frame,fragment);
                          fragmentTransaction.commit();
                          return true;
  
                      // For rest of the options we just show a toast on click
  
                      case R.id.starred:
  
                          startActivity(new Intent(MainActivity.this,Activity2.class));
                          drawerLayout.closeDrawers();
                          Toast.makeText(getApplicationContext(),"Stared Selected",Toast.LENGTH_SHORT).show();
                          return true;
                      case R.id.sent_mail:
                          Toast.makeText(getApplicationContext(),"Send Selected",Toast.LENGTH_SHORT).show();
                          return true;
                      case R.id.drafts:
                          Toast.makeText(getApplicationContext(),"Drafts Selected",Toast.LENGTH_SHORT).show();
                          return true;
                      case R.id.allmail:
                          Toast.makeText(getApplicationContext(),"All Mail Selected",Toast.LENGTH_SHORT).show();
                          return true;
                      case R.id.trash:
                          Toast.makeText(getApplicationContext(),"Trash Selected",Toast.LENGTH_SHORT).show();
                          return true;
                      case R.id.spam:
                          Toast.makeText(getApplicationContext(),"Spam Selected",Toast.LENGTH_SHORT).show();
                          return true;
                      default:
                          Toast.makeText(getApplicationContext(),"Somethings Wrong",Toast.LENGTH_SHORT).show();
                          return true;
  
                  }
              }
          });
  
          // Initializing Drawer Layout and ActionBarToggle
          drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
          ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){
  
              @Override
              public void onDrawerClosed(View drawerView) {
                  // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
                  super.onDrawerClosed(drawerView);
              }
  
              @Override
              public void onDrawerOpened(View drawerView) {
                  // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
  
                  super.onDrawerOpened(drawerView);
              }
          };
  
          //Setting the actionbarToggle to drawer layout
          drawerLayout.setDrawerListener(actionBarDrawerToggle);
  
          //calling sync state is necessay or else your hamburger icon wont show up
          actionBarDrawerToggle.syncState();
  
  
  
  
  
  
      }
  }
  </code></pre>
  
  <p>Activity2.java</p>
  
  <pre><code>public class Activity2 extends MainActivity{
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          FrameLayout frameLayout = (FrameLayout)findViewById(R.id.frame);
          // inflate the custom activity layout
          LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View activityView = layoutInflater.inflate(R.layout.activity_2, null,false);
          // add the custom layout of this activity to frame layout.
          frameLayout.addView(activityView);
      }
  }
  </code></pre>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/drawer"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:context=".MainActivity"&gt;
  
      &lt;LinearLayout
          android:layout_height="match_parent"
          android:layout_width="match_parent"
          android:orientation="vertical"
          &gt;
          &lt;include
              android:id="@+id/toolbar"
              layout="@layout/tool_bar"
          /&gt;
          &lt;FrameLayout
              android:id="@+id/frame"
              android:layout_width="match_parent"
              android:layout_height="match_parent"&gt;
  
          &lt;/FrameLayout&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;android.support.design.widget.NavigationView
          android:id="@+id/navigation_view"
          android:layout_height="match_parent"
          android:layout_width="wrap_content"
          android:layout_gravity="start"
          app:headerLayout="@layout/header"
          app:menu="@menu/drawer"
          /&gt;
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p>drawer.xml(menu_drawer)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  
      &lt;group android:checkableBehavior="single"&gt;
  
          &lt;item
              android:id="@+id/inbox"
              android:checked="false"
              android:icon="@drawable/ic_inbox_black"
              android:title="@string/inbox_string" /&gt;
  
          &lt;item
              android:id="@+id/starred"
              android:checked="false"
              android:icon="@drawable/ic_star_black"
              android:title="@string/starred_string" /&gt;
  
          &lt;item
              android:id="@+id/sent_mail"
              android:checked="false"
              android:icon="@drawable/ic_send_black"
              android:title="@string/sent_mail_string" /&gt;
  
          &lt;item
              android:id="@+id/drafts"
              android:checked="false"
              android:icon="@drawable/ic_drafts_black"
              android:title="@string/draft_string" /&gt;
  
  
          &lt;item
              android:id="@+id/allmail"
              android:checked="false"
              android:icon="@drawable/ic_email_black"
              android:title="@string/all_mail_string" /&gt;
          &lt;item
              android:id="@+id/trash"
              android:checked="false"
              android:icon="@drawable/ic_delete_black"
              android:title="@string/trash_string" /&gt;
          &lt;item
              android:id="@+id/spam"
              android:checked="false"
              android:icon="@drawable/ic_error_black"
              android:title="@string/spam_string" /&gt;
  
      &lt;/group&gt;
  &lt;/menu&gt;
  </code></pre>