newbieCoder.pkg
newbieCoder.pkg

Reputation: 277

How to pass ArrayList<Fragment>?

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?

Upvotes: 0

Views: 491

Answers (3)

newbieCoder.pkg
newbieCoder.pkg

Reputation: 277

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();
}

Upvotes: 0

Ramesh Yankati
Ramesh Yankati

Reputation: 1217

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

Upvotes: 0

Greg Moens
Greg Moens

Reputation: 1825

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.

Upvotes: 2

Related Questions