Android Pass Data from Activity to Fragment - Androhub

Fragment_Banner

Android Pass Data from Activity to Fragment

Earlier we had learn how to pass data from one activity to another and get back data from another activity. You can find both tutorials here :

  1. Android Intent.
  2. StartActivityForResult.

Today we are going to learn how to pass data from Activity to Fragment. Before starting this tutorial just go through Fragments Tutorial if you are new to Fragments. For passing data from activity to fragment you can follow some steps given below:

  • Bundle – Bundle is a mapping from String values to various Parcelable types.

We can pass int, float or any data type. This is same as we pass data in Intent.

  • SetArguments – setArguments() is a method to set bundle data over fragment. This bundle data will pass with the fragment.

Note: The above two steps are done inside Activity.
Now, steps for how to retrieve data inside fragment.

  • GetArguments – getArguments() method used for getting passed data from activity to fragment. In this we have to retrieve data with particular key value.

VIDEO DEMO

Now, Let’s start with example.

1. Create a new project in Android Studio by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. Now create a xml layout naming activity_main.xml and add the following code. In this layout i had taken two Button one for Simple Fragment and another for Argument Fragment and there is a FrameLayout for holding fragment.

3. For fragment UI design create new layout xml file naming fragmentlayout.xml and add the following code. In this layout i had taken one TextView whose text is “Default Text” by default which gonna change while replacing Argument Fragment.

4. Since we are working on two fragment so create two new java classes. First create DefaultFragment.java and add the following code. In this code i had just inflate the layout.

5. Now create a new java class for second fragment naming ArgumentFragment.java and add the following code. In this code i am retrieving passed data by using getArguments() method with particular key value and setting over TextView.

6. Finally come to your MainActivity.java and add the following code. In this code i am passing data by using setArguments() to fragment.

7. Finally, all done  –  run the app and you will get the output as shown in video.

Thanks. :)

 

25 Comments

MJ_IT
Friday, September 16th, 2016

Working for me! Thanks a lot !

fajarra
Monday, February 6th, 2017

Its work. How about passing data from fragment to activity ?,

Dr. Droid
Monday, February 6th, 2017

Hi Fajarra,

Its same as like we use to send data from one activity to another.

Intent in = new Intent(getActivity(),Activity.class);
in.putExtra(key,value);
startActivity(in);

Thanks

Shreya Singh
Friday, June 23rd, 2017

When I am doing the same with int my app is carshing, can u plz help me out?

Dr. Droid
Friday, June 23rd, 2017

Hi Shreya,

Can you share your code with me , so that i can understand where exactly you are doing wrong.

Thanks

nitish sharma
Tuesday, November 14th, 2017

how to send data from fragment to activity ?

Dr. Droid
Tuesday, November 14th, 2017

Hi Nitish,

Use the below code to send data to activity from fragment :

Intent in = new Intent(getActivity(),ActivityName.class);
in.putExtra(“message”,message);
startActivity(in);

Thanks

vips
Tuesday, December 12th, 2017

great one bro works fine

Andrea
Friday, December 15th, 2017

Do you know what is the best way to send and fetch data between fragments?
If I have a list of posts for example and I want to show the full post in another fragment when I click on it, what is the best way to do that, send all data from one fragment to another, (title, image url, text) or send just the ID and fetch the rest inside the new fragment?
Regards,
Andrea

Dr. Droid
Saturday, December 16th, 2017

Hi Andrea,

To send data from one Fragment to another Fragment you have to use the same thing that i have done in this above article by passing data from Activity to Fragment.

So you can use the same code and put in your Fragment from where you want to pass data to another Fragment.

Thanks

Shivani Verma
Monday, February 26th, 2018

sir i have an issue i am getting a list of songs in json response and there is a parameter inside it song category which includes different categories like hot tracks,trending etc and now i have to display them in different fragments according to their respective categories and i dont know hoe to achieve that can you please help me out

Dr. Droid
Wednesday, February 28th, 2018

Hi Shivani

Are you able to parse JSON data?

How you want to show your categories and songs? Using tabs or some other designs?

Thanks

ADAM
Thursday, June 21st, 2018

i have a list of books which i used parcelable to pass it to another fragment from my class. i get getArguments as null in my fragment when i try to retrieve it , causing an NPE and crashes my app. what should i do ?

Dr. Droid
Sunday, June 24th, 2018

Hi Adam,

Can you share your code to my mail id. So that i can see what is the issue.

Thanks

akash
Thursday, October 4th, 2018

i am getting arguments null

Dr. Droid
Monday, October 8th, 2018

Hi Akash,

Can you share your error logs and code line where you are getting exception. May be you are doing some wrong.

Thanks

Claudio
Sunday, March 31st, 2019

Thanks for sharing your knowledge. I was wondering how could you pass an array of objects as argument, but it worked fine…
Best regards from Brazil!

Romelo Lora
Thursday, November 7th, 2019

hi how can I move the data from activity to fragment

Dr. Droid
Thursday, November 7th, 2019

Hi Romelo,

You have to use the same way which is shown in the post. I am posting code snippet below:

Fragment argumentFragment = new ArgumentFragment();//Get Fragment Instance
Bundle data = new Bundle();//Use bundle to pass data
data.putString(“data”, “This is Argument Fragment”);//put string, int, etc in bundle with a key value
argumentFragment.setArguments(data);//Finally set argument bundle to fragment

fragmentManager.beginTransaction().replace(R.id.fragmentContainer, argumentFragment).commit();//now replace the argument fragment

Thanks

newbe
Friday, November 8th, 2019

how can i go back from an AppCompatActivity to Fragments

Dr. Droid
Saturday, November 9th, 2019

Hi Newbe,

For your question there are two situations:
1. If the fragment is in the same Activity: Then you have to use the fragment transaction.
2. If the fragment is in different Activity: Then you have to use Intent and once you go to different activity there in onCreate method you have to replace/add respective fragment.

Let me know if you have any other case or situation.

Thanks

Jayant
Tuesday, December 24th, 2019

Can you please tell this for Kotlin

Dr. Droid
Wednesday, December 25th, 2019

Hi Jayant,

Please check this link: https://stackoverflow.com/questions/53964718/passing-a-value-from-activity-to-fragment-in-kotlin.

Thanks

Chen Vincent
Thursday, February 20th, 2020

Hi Dr. Droid: I am using navigation drawer model in my APP. And I need share the data from MainActivity to other fragments. How can I do it? May I try to use the example you posted to this scenario? Thank you for your reply.

Dr. Droid
Thursday, February 20th, 2020

Hi Chen,

Yes, you can use this example to share data between activity and fragment.

Thanks

Post comment

Your email address will not be published. Required fields are marked *