0

To avoid creating multiple activities I have one Activity2 that has this code that allows me to just pass an array of Fragments from any Activity I want.

privateArrayList<Fragment> fragArrayList;

fragArrayList = intent.getParcelableArrayListExtra("fragArrayList");

Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          setFragment(fragArrayList.get(i));
        }
    });

private void setFragment(Fragment fragment) {
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .replace(R.id.layout, fragment)
            .addToBackStack(null)
            .commit();
}

In Activity1 I have this code (That I know doesn't work)

Wrong 2nd argument type. Found: 'java.util.ArrayList<android.support.v4.app.Fragment>', required: 'java.util.ArrayList<? extends android.os.Parcelable>'

.

Intent intent= new Intent(Activity1.this, Activity2.class);

ArrayList<Fragment> fragArrayList = new ArrayList<>();
                fragArrayList.add(new frag1());
                fragArrayList.add(new frag2());
                fragArrayList.add(new frag3());
                fragArrayList.add(new frag4());
                fragArrayList.add(new frag5());

                intent.putParcelableArrayListExtra("fragArrayList"), fragArrayList);

The point is to make fragArrayList travel from any Activity to Activity2. And since the frags inside fragArrayList will be different depending on the activity they are coming from I cant just add them to the ArrayList inside Activity2.

And since there will be several activities is not efficient to create logic inside the Activity2 to handle from each Activity the user came from.

How can I pass the ArrayList from Activity1 to Activity2?

2

3 Answers 3

2

Fragments are not Parcelable, so you won't be able to pass them via intent. You could pass the fully qualified fragment class name via intent and then instantiate the fragment(s) in Activity2 via reflection.

Sign up to request clarification or add additional context in comments.

2 Comments

You can also build an ArrayList of Class objects, which are serializable, then use putExtra(name, arrayList), recover them with getSerializable(name), and then build the fragments with reflection directly from the class object with newInstance().
Thank you for your answers I did it!
0

That's a bad design passing list of fragment objects and you could only pass array of Parcelable class of object.

Comments

0

Success Thank you to: Greg Moens & gicci for the insight

Activity1

Intent intent= new Intent(Activity1.this, Activity2.class);

ArrayList<String> fragArrayList= new ArrayList<>();

fragArrayList.add(String.valueOf(frag1.class.getName()));
fragArrayList.add(String.valueOf(frag2.class.getName()));
fragArrayList.add(String.valueOf(frag3.class.getName()));
fragArrayList.add(String.valueOf(frag4.class.getName()));
fragArrayList.add(String.valueOf(frag5.class.getName()));

intent.putExtra("fragArrayList"), fragArrayList);

Activity2

ArrayList<String> fragArrayList = new ArrayList<>();

fragArrayList = intent.getStringArrayListExtra("fragArrayList");

Button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      setFragment(fragArrayList.get(i));
    }
});

 private void setFragment(String fragClass) {

    Fragment fragment = Fragment.instantiate(this, fragClass);

    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .replace(R.id.layout, fragment)
            .addToBackStack(null)
            .commit();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.