I want to save and use Activity in putSerializable

Asked 2 years ago, Updated 2 years ago, 74 views

You are about to pass an instance of Activity to Fragment, and the newInstance method:

public static DailyFragment newInstance (Activity activity) {
        DailyFragment dailyFragment = new DailyFragment();
        Bundle bundle=new Bundle();
        bundle.putSerializable("activity", newObject[]{activity});
        dailyFragment.setArguments(bundle);
        return dailyFragment;
    }

And when I restored Activity, I did the following:

 (Activity) getArguments().getSerializable("activity")

However, the error java.lang.Object[]cannot be cast to android.app.Activity appears.
How can I save and use Activity in newInstance using putSerializable?
Please let me know if anyone knows.I'm sorry, but I appreciate your cooperation.

android fragment

2022-09-30 20:36

2 Answers

Nice to meet you.
The contents of the error say that the array of objects cannot be cast in Activity.
I can't do this.Activity is only an object child.

Also, Activity does not implement Serializable itself, so get it from putSerializable
I don't think it's possible toParcelable is not implemented either.

What exactly do you want to save about Activity?
If you want to save the Activity state, you can use the bundle itself that you can retrieve in the onSaveInstanceState. It can also be stored in a bundle.You can also put a bundle in the bundle.

I would appreciate it if you could refer to it.


2022-09-30 20:36

serializeOnly classes that implement the Serializable interface (implementations) can be used.
The Activity class does not implement the Serializable interface and cannot be putSerializable.
Even if Activity implements Serializable, it becomes a different instance when getSerializable.

Why do I need an instance of Activity?
Can't I do getActivity() with Fragment?


2022-09-30 20:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.