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?