I pass data to the Activity via the Bundle with NavigationDrawer in the Fragment, but when reading the Bundle it is null

Activity.class

public class MainActivity extends ActionBarActivity { private DrawerLayout myDrawerLayout; private ListView myDrawerList; private ActionBarDrawerToggle myDrawerToggle; private CharSequence myDrawerTitle; private CharSequence myTitle; private String[] viewsNames; public FragmentManager passfragmentManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myTitle = getTitle(); myDrawerTitle = getResources().getString(R.string.menu); passfragmentManager = getSupportFragmentManager(); viewsNames = getResources().getStringArray(R.array.views_array); myDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); myDrawerList = (ListView) findViewById(R.id.left_drawer); myDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, viewsNames)); android.support.v7.app.ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); myDrawerToggle = new ActionBarDrawerToggle(this, myDrawerLayout, R.string.open_menu, R.string.close_menu) { public void onDrawerClosed(View view) { getSupportActionBar().setTitle(myTitle); invalidateOptionsMenu(); } public void onDrawerOpened(View drawerView) { getSupportActionBar().setTitle(myDrawerTitle); invalidateOptionsMenu(); } }; myDrawerLayout.setDrawerListener(myDrawerToggle); if (savedInstanceState == null) { displayView(0); } myDrawerList.setOnItemClickListener(new DrawerItemClickListener()); } private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick( AdapterView<?> parent, View view, int position, long id ) { displayView(position); } } private void displayView(int position) { Fragment fragment = null; switch (position) { case 0: fragment = new FirstFragment(); break; case 1: // sendToFragmentBundle(); fragment = new SecondFragment(); sendToFragmentBundle(); break; case 2: fragment = new ThirdFragment(); break; default: break; } if (fragment != null) { FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.content_frame, fragment).commit(); myDrawerList.setItemChecked(position, true); myDrawerList.setSelection(position); setTitle(viewsNames[position]); myDrawerLayout.closeDrawer(myDrawerList); } else { Log.e("MainActivity", "Error in creating fragment"); } } public void sendToFragmentBundle() { //---------------------------------------- Bundle bundle=new Bundle(); bundle.putString("name", "<<<<<<I Am Bundle From Activity it is WORK >>>>>>>>"); SecondFragment fragobj=new SecondFragment(); fragobj.setArguments(bundle); getSupportFragmentManager().beginTransaction() .replace(R.id.content_frame, fragobj) .commit(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (myDrawerToggle.onOptionsItemSelected(item)) { return true; } switch (item.getItemId()) { case R.id.action_settings: return true; default: return super.onOptionsItemSelected(item); } } @Override public boolean onPrepareOptionsMenu(Menu menu) { boolean drawerOpen = myDrawerLayout.isDrawerOpen(myDrawerList); menu.findItem(R.id.action_settings).setVisible(!drawerOpen); return super.onPrepareOptionsMenu(menu); } @Override public void setTitle(CharSequence title) { myTitle = title; getSupportActionBar().setTitle(myTitle); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); myDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); myDrawerToggle.onConfigurationChanged(newConfig); } } 

Fragment.class TextView displays "BUNDLE NULL"

 public class SecondFragment extends Fragment { final String LOG_TAG = "myLogs_SecondFragment"; Bundle bundle; TextView txtFrg; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_second, container, false); txtFrg = (TextView) rootView.findViewById(R.id.txtLabel); bundle = this.getArguments(); if (bundle != null) { String myValue = bundle.getString("name"); Log.d(LOG_TAG, " ----------->Fragment myBundle contain " + myValue ); txtFrg.setText("Bundle contain"+myValue); } else { Log.d(LOG_TAG, " ----------->Fragment myBundle contain " + "NULL"); txtFrg.setText("BUNDLE NULL"); } return rootView; } } 

    1 answer 1

    Right. It should be so. In the sendToFragmentBundle procedure, you create a fragment, assign an argument, and output the fragment in an activit. Further, at the end of the displayView procedure, you output a completely different fragment to the activation:

     case 1: // sendToFragmentBundle(); fragment = new SecondFragment(); sendToFragmentBundle(); break; 

    * your code *

     if (fragment != null) { FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.content_frame, fragment).commit(); 

    * your code *

    You need to redo the sendToFragmentBundle procedure. Either by giving it the created fragment in the parameters, or convert the procedure into a function.

     public Fragment sendToFragmentBundle() { //---------------------------------------- Bundle bundle=new Bundle(); bundle.putString("name", "<<<<<<I Am Bundle From Activity it is WORK >>>>>>>>"); SecondFragment fragobj=new SecondFragment(); fragobj.setArguments(bundle); return fragobj; } case 1: // sendToFragmentBundle(); fragment = sendToFragmentBundle(); break;