Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send data from activity to fragment in Android

I have two classes. First is activity, second is a fragment where I have some EditText. In activity I have a subclass with async-task and in method doInBackground I get some result, which I save to variable. How can I send this variable from subclass "my activity" to this fragment?

like image 726
user1302569 Avatar asked Oct 05 '12 05:10

user1302569


People also ask

How pass data from activity to fragment in Android using intent?

This example demonstrates how do I pass a variable from activity to Fragment in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

From Activity you send data with intent as:

Bundle bundle = new Bundle(); bundle.putString("edttext", "From Activity"); // set Fragmentclass Arguments Fragmentclass fragobj = new Fragmentclass(); fragobj.setArguments(bundle); 

and in Fragment onCreateView method:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState) {     String strtext = getArguments().getString("edttext");         return inflater.inflate(R.layout.fragment, container, false); } 
like image 125
ρяσѕρєя K Avatar answered Sep 19 '22 23:09

ρяσѕρєя K