I want to pass a data from activity to fragment. The activity which i have created is navigation drawer activity. Once i have click on menu, it will direct to the fragment page. would like to ask how to pass different data to the same fragment?
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
Class fragmentClass = null;
Bundle bundle = new Bundle();
if (id == R.id.frag_one) {
fragmentClass = Fragment1.class;
} else if (id == R.id.frag_two) {
fragmentClass = Fragment2.class;
} else if (id == R.id.frag_three) {
fragmentClass = Fragment3.class;
} else if (id == R.id.frag_four) {
fragmentClass = Fragment4.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}