Send string parameter in Activity to Fragment On Android

This time I will discuss how do I send a string parameter of activity to fragment the turn layout (Intent).  I experienced this problem just now because I have just started using a fragment in my activity.

What is the fragment?

Fragment basically like activity that handle a layout, but the difference is the fragment can not walk alone fragment need activity to show a component.

We further discussed how to send a string parameter of activity to another activity that use fragment.

So I have a second activity that one without the other fragment by fragment, on the Activity 1 is currently set at the event onclick button perform as usual intent.

Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("valueString", message);
startActivity(intent);

Kemudian pada Activity2 getting value string dari Activity1.

Intent intent = getIntent();
String strValue = intent.getStringExtra("valueString");

And to send the String parameter value to add syntax fragment such as the following syntax previous intent.

FragmentClass classFragement = new FragmentClass ();
Bundle bundle = new Bundle();
bundle.putString("valueString", strValue);
classFragement .setArguments(bundle);

And for the class fragment add the following syntax.

Bundle bundle = getArguments();
String selected = getActivity().getIntent().getStringExtra("valueString");
textValue.setText(selected);

In this tutorial I show you the results of parameters in a EditText that I named textValue.

okay so this tutorial may be useful: D

 

 

Leave a comment