0

I am trying to build simple movies name list in recyclerview inside drawer layout when I click any movie name show me the detail of movie like an image, textView in another fragment. How to implement a bundle for sending data to fragment from an activity.This is my main activity

ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24
  • 1
    Duplicate : https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android – Shayan Tabatabaee Oct 02 '18 at 07:41
  • 3
    Possible duplicate of [Send data from activity to fragment in android](https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android) – Arty Oct 02 '18 at 09:11

2 Answers2

1

You can send data from your activity to fragment like this:

In your MainActivity , add fragment when you want to open detail fragment

 addFragment(R.id.frame_container,MovieDetailFragment.getInstance(/*your data*/),tag_name);
 //then commit

In your Movie Detail Fragment,

 public static MovieDetailFragment(//your data//){
   MovieDetailFragment detailFragment  new MovieDetailFragment();
   Bundle bundle = new Bundle();
   bundle.putString(key,//yourdata//);//you can use any type you want to put in bundle
   detailFragment.setArguments(bundle);
   return movieDetailFragment;
 }

and in your MovieDetailFragment's onCreateView() , you can get your data from bundle like this:-

  if(getArguments()!=null){
    String data = getArguments().getString(key);
  }

I hope it will solve your query!!

Abhishek Sharma
  • 271
  • 2
  • 11
0

Step 1: Passing the data from activity to fragment,

Bundle bundle = new Bundle();
bundle.putString("params", "My String data");
set MyFragment Arguments
MyFragment myObj = new MyFragment();
myObj.setArguments(bundle);

Step 2: Receiving the data to the fragment,

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString("params");
        }
    }
No Body
  • 671
  • 5
  • 14