title: Send data from Activity to fragment in an Android app
body: <p>In my application I have an Activity (MainScreen) and one Fragment (Orders). I have a string (name) in the Activity and I want to send it to the Fragment. (Under the comment lines you can see my attempt to send the string from the activity to the fragment). I don't know why, but the application crashes with this.</p>
  
  <p>This is MainScreen.java:</p>
  
  <pre><code>public class MainScreen  extends AppCompatActivity {
  
      Switch list_toggle;
      String user_id,username,status;
      boolean ischecked;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main_screen);
  
          final TextView textView4 = (TextView) findViewById(R.id.textView4);
          final TextView textView3 = (TextView) findViewById(R.id.textView3);
  
          list_toggle = (Switch) findViewById(R.id.mySwitch);
  
          Bundle extras = getIntent().getExtras();
          if (extras != null) {
  
              username = extras.getString("USER_NAME");
              user_id = extras.getString("USER_ID");
              status = extras.getString("STATUS");
          }
  
          if (status.contentEquals("Available")){
  
              ischecked = true;
          }
          else{
              ischecked = false;
          }
  
          textView4.setText(Html.fromHtml(status));
          list_toggle.setChecked(ischecked);
  
  
          String[] parts = username.split(" ");
          String name = parts[0]; // 004
          String surname = parts[1];
  
          textView3.setText(Html.fromHtml("Hi, " + name + " "));
  
  
          list_toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  if (isChecked) {
  
                      textView4.setText(Html.fromHtml("Available"));
  
                  } else {
  
                      textView4.setText(Html.fromHtml("Unavailable"));
  
                  }
              }
          });
  
          //I tried to do this to send "name" to the fragment
          Bundle bundle = new Bundle();
          bundle.putString("name", name);
          Orders fragobj = new Orders();
          fragobj.setArguments(bundle);
  
          TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
          tabLayout.addTab(tabLayout.newTab().setText("Orders"));
          tabLayout.addTab(tabLayout.newTab().setText("Past Orders"));
          tabLayout.addTab(tabLayout.newTab().setText("More"));
          tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
  
          final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
          final PagerAdapter adapter = new PagerAdapter
                  (getSupportFragmentManager(), tabLayout.getTabCount());
          viewPager.setAdapter(adapter);
          viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
          tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
  
  
              @Override
              public void onTabSelected(TabLayout.Tab tab) {
                  viewPager.setCurrentItem(tab.getPosition());
              }
  
              @Override
              public void onTabUnselected(TabLayout.Tab tab) {
  
              }
  
              @Override
              public void onTabReselected(TabLayout.Tab tab) {
  
              }
          });
  
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
  }
  </code></pre>
  
  <p>And this is Orders.java:</p>
  
  <pre><code>public class Orders extends Fragment {
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View v = inflater.inflate(R.layout.orders, container, false);
          ListView listView1 = (ListView) v.findViewById(R.id.listView1);
          TextView textView3 = (TextView) v.findViewById(R.id.textView16);
  
  
          String currentDate = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()).format(new Date());
          textView3.setText(Html.fromHtml(currentDate));
  
          //I tried to do this to get the String name from the Activity
          String data = getArguments().getString("name");
          System.out.println(data);
  
          Order[] items = {
                  new Order("#403", "07-04-2016", "5:29 PM"),
                  new Order("#404", "07-04-2016", "5:35 PM"),
                  new Order("#405", "07-04-2016", "5:40 PM"),
                  new Order("#406", "07-04-2016", "5:54 PM"),
          };
  
          ArrayAdapter&lt;Order&gt; adapter = new ArrayAdapter&lt;Order&gt;(getActivity(),
                  android.R.layout.simple_list_item_1, items);
  
          listView1.setAdapter(adapter);
  
          listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position,
                                      long id) {
                  String item = ((TextView) view).getText().toString();
  
  
                  Intent intent = new Intent(getActivity(), OrderDetails.class);
                  intent.putExtra("data",item);
                  startActivity(intent);
  
              }
          });
  
          return v;
      }
  }
  </code></pre>