Send a data of variable from onCreate() to Fragment

I have a problem with my Android Studio project. I have a BottomNavgationActivity with 4 fragments. This activity starts from LoginActivity, which send an Email value to BottomNavigationActivity like this:

Intent main = new Intent(LoginActivity.this, BottomNavigationActivity.class);
main.putExtra("EMAIL", email);
startActivity(main);

In BottomNavigationActivity i call this data like this:

String email;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bottom_navigation);
    Intent intent = getIntent();
    email = intent.getStringExtra("EMAIL");

I need to send this "email" data to a SettingsFragment, which user can choose from BottomNavigationBar. For this i have onNavigationItemSelected where i call selectedFragment = choosen fragment from BottomNavigation. I’ve tried something like this:

case R.id.navigation_settings:
                selectedFragment = new SettingsFragment();
                bundle.putString("EMAIL", email);
                selectedFragment.setArguments(bundle);
                break;

But

 String email = this.getArguments().getString("EMAIL");

in SettingsFragment returns me null. What should i do?

Source: Android Questions

LEAVE A COMMENT