Android Pass Data from Activity to Fragment

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. :)

Download Source Code

 

About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.

COMMENTS (16)
Reply

Working for me! Thanks a lot !

Reply

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

    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

Reply

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

    Hi Shreya,

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

    Thanks

Reply

how to send data from fragment to activity ?

    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

Reply

great one bro works fine

Reply

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

    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

Reply

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

    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

Reply

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 ?

    Hi Adam,

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

    Thanks

Reply

i am getting arguments null

    Hi Akash,

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

    Thanks

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)