0

I have a class called "Core" which contains many classes such as "paths", "session", etc, etc. I am in Activity 1, when passing Activity 2 I reload all core data? (there are many), I need to pass data by data of all kinds through intent.putExtras (Bundle)? there any way to pass the "core" class? I tried to do that "core" is serializable but the compiler tells me it is impossible to transform the class serializable. I must charge the "core" class whenever changing activity? not do this much slower navigation? most core data obtained from the internet among many other very slow mathematical calculations which I figured I could make a unique look in Activity 1 and just after all the data out through arguments but does not work :(

Intent intent = new Intent(this, ActivityPanel1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Bundle argumentos = new Bundle();
argumentos.putSerializable("core", this.core); // <- The problem
intent.putExtras(argumentos);

startActivity(intent);

Result:

at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.NotSerializableException: com.example.splash.SplashActivity
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
e-info128
  • 3,727
  • 10
  • 40
  • 57
  • When you say your Core class contains many classes, u mean it contains objects of other classes. Right??? – Shadab Mirza Dec 12 '14 at 20:35
  • Yes, example: class session, class path, class configs, class roads, etc etc, in the core use vars: this.session = new Session(), etc etc, for example call from activity: this.core.session.login(user, pass); if(this.core.session.errors.has()){ ... the errors is a class Errors with The List class. – e-info128 Dec 12 '14 at 20:44
  • when you implement serializable on a class, all the objects in the class should also implement serializable. In your case Core class should implement Serializable, if it contains object of class Session, then Session class should also implement serializable and so on. – Shadab Mirza Dec 12 '14 at 22:31

3 Answers3

0

To pass data (objects) between two activities using intents you have to implement the Core class with Serializable or Parcellable(recommended). Otherwise you can save the data in application class (the class which extends Application) then you can use it whenever you required.

Chandrakanth
  • 3,711
  • 2
  • 18
  • 31
  • Parcellable Serializable and are not an option since my class gets subclasses and these implementations do not support it. I am trying to use static variables aver as I find it. I think that officially there is no solution to this, as will other developers? will work only with serializable classes with flat data? in android major classes are not used with lots of subclasses as frameworks? – e-info128 Dec 12 '14 at 19:39
0

I don't recommend this unless you really need to do it, but one way around your problem is to implement an Application class, which will have the same lifetime as the application, and then temporarily store the data to be communicated there.

The second activity can then retrieve it from the Application class.

Again, this is an ugly hack that shouldn't be undertaken lightly. I'm personally trying to remove this behavior from a codebase that I inherited.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • good idea even thought doing communicating through a service that accompanies all activities but like you said try not to do so unless strictly necessary (I hope not) – e-info128 Dec 12 '14 at 19:32
0

You can get references from here. Hope it will help you. Data can be send using serialized or parcelable

Passing data through intent using Serializable

creating parcelable in android from some of the fields of a class

Community
  • 1
  • 1
bond007
  • 2,434
  • 1
  • 17
  • 17
  • The problem is that the core class has other classes, I understand that serializable and parcellable only be used for classes with flat data such as strings, int, etc, but not objects, that's why android studio gave me this exception when I tried to implement from parcellable. – e-info128 Dec 12 '14 at 19:34