-1

How to pass String[] from MainActivity

example.add(new Fragment1(new String[]{"answer 1", "answer 2", "answer 3"}));
example.add(new Fragment1(new String[]{"answer 4", "answer 5", "answer 6"}));

to this Fragment and make it work dynamically (on swipe)

public class Fragment1 extends Fragment {

    String stringValue;
    int imagesResId;
    TextView text;
    String[] rbData;

    public Fragment1() {
    }

    public Fragment1(String str, int imageView, String[] rb) {
        this.stringValue = str;
        this.imagesResId = imageView;
        this.rbData = rb;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment1, container, false);
        text = view.findViewById(R.id.textView);
        ImageView imageResId = view.findViewById(image);

        text.setText(stringValue);
        imageResId.setImageResource(imagesResId);

        return view;
    }
}
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50

1 Answers1

0

To pass:

putStringArray("my_array", myArray);

To retrieve:

getArguments().getStringArray("my_array");

In your fragment:

public static Fragment1 newInstance(String[] array) {
    Fragment1 fragment = new Fragment1();
    Bundle args = new Bundle();
    args.putStringArray("my_array", array);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        String[] myArray = getArguments().getStringArray("my_array");
    }
}

To start the fragment:

Fragment1 frag = Fragment1.newInstance(myArray);
FragmentTransaction ft = getFragmentManager().beginTransaction();
//.....
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
  • Thank you. I will try this. But instead of using `bundle`, is there any other way to pass `StringArray` directly from `Activity` and `Fragment1` constructor to `onCreateView` method? Like i did with e.g. `text` in my above post? Line of code from `MainActivity` is `example.add(new Fragment1("text1"));` – MissAndroid Oct 24 '17 at 20:35
  • @MissAndroid Using `Bundle` to pass data from `Activity` to `Fragment` or `Fragment` to `Fragment` is the probably the way to do. But depending on the use case you can use `interface` or use some third party libraries like event bus etc. if you don't want to use `Bundle`. – Nongthonbam Tonthoi Oct 24 '17 at 20:41
  • I mean `bundle` is ok, but just wondering if i manged to pass "text" from this line `example.add(new Fragment1("text1"));` in `MainActivity` to `Fragment1` using constructor with arguments, so maybe there is a way to do the same with `String[] answer1..etc` – MissAndroid Oct 24 '17 at 20:49
  • Please see [this](https://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment) – Nongthonbam Tonthoi Oct 24 '17 at 21:02
  • hi there. could you please have a look at this post? have some troubles setting boolean value in `onCreateView` and `if` statement. https://stackoverflow.com/questions/47062603/passing-different-images-every-second-fragment – MissAndroid Nov 02 '17 at 17:33