Passing custom List of data using Bundle
args.putParcelableArrayList(DATA_KEY, new ArrayList<>(data));
Make your NewObjectClass
Parcelable
or Serializable
and then create a new class, effectively, containing your list, also Parcelable
or Serializable
. Then use Bundle.putSerializable
(or putParcelable
)
Or, simpler, make NewObjectClass
Parcelable
then use putParcelableArrayList
if you can do with ArrayList
instead of generic List
Or, simplest, make NewObjectClass
Serializable and use putSerializable
passing ArrayList<NewObjectClass>
because ArrayList
is Serializable
In the last case perhaps you only will have to ad implements
Serializable
to your class.
Alternatively, if your data seem to be large, consider keeping them in a custom Application
-derived object instead. You extend Application
and then such object will exist all the time your app exist. Don't forget to register it in manifest.
class MyApplication extends Application {
public static Object myData;
}
Or you can do with shared preferences
PreferenceManager.getDefaultSharedPreferences().edit().putInt("a", 1).commit();
PreferenceManager.getDefaultSharedPreferences().getInt("a");
use putSerializable
method to pass your custom list.
args.putSerializable(KEY, ArrayList<Type>);
and fetch it using getSerializable
ArrayList<Type> list = (ArrayList<Type>) getArguments().getSerializable(KEY);