There is a class MainActivity, it has a method

public boolean onNavigationItemSelected(MenuItem item), 

which is responsible for launching activities or fragments depending on the selected item in the menu. There is one fragment in which you need to transfer the value 1 and 2.

 public boolean onNavigationItemSelected(MenuItem item) { int id = item.getItemId(); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); FragmentLearnWords fragLearn = new FragmentLearnWords(); Bundle bundle = new Bundle(); if (id == R.id.nav_learn_words) { bundle.putInt("key", 1); ft.replace(R.id.mainFrame, fLearnWords); } else if (id == R.id.nav_repetition_words) { bundle.putInt("key", 2); ft.replace(R.id.mainFrame, fLearnWords); } fragLearn.setArguments(bundle); ft.commit(); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } 

In the snippet, the code is:

 Bundle bundle = this.getArguments(); if (bundle != null) { int i = bundle.getInt("key", 0); Toast.makeText(getContext(), ""+i, Toast.LENGTH_SHORT).show(); } 

Toast is not displayed.
What can be a mistake?

Information taken from this topic

    1 answer 1

    This date is fragLearn.setArguments(bundle); must be called before you display the fragment, i.e. before calling ft.replace(R.id.mainFrame, fLearnWords);

    I also noticed that you decided to "wrap" the insertion of a fragment into a transaction. You can make it much easier and will look better.
    Something like this

     public boolean onNavigationItemSelected(MenuItem item) { int id = item.getItemId(); FragmentLearnWords fragLearn = new FragmentLearnWords(); Bundle bundle = new Bundle(); if (id == R.id.nav_learn_words) { bundle.putInt("key", 1); } else if (id == R.id.nav_repetition_words) { bundle.putInt("key", 2); } fragLearn.setArguments(bundle); getSupportFragmentManager() .beginTransaction() .replace(R.id.mainFrame, fLearnWords) .commit(); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } 

    I also advise for better performance, pre-initialize the drawer, and not to pull findViewById(R.id.drawer_layout); each time findViewById(R.id.drawer_layout); , because findViewById heavy operation and it is better to perform it once.