1

My app contains 2 activitys. Activity A is the one which is created by starting the app. In this one I create an object of my own class MyClass. This class contains one string and 3 integers. In activity A this object gets written. The second activity B needs to read this object. How can I pass it from A to B? Or is there an other solution?

LosTheAlef
  • 25
  • 5
  • [here](http://stackoverflow.com/questions/15747727/pass-arraylist-of-user-defined-objects-to-intent-android/15747819#15747819) check this answer. – Simon Dorociak Apr 05 '13 at 13:23

3 Answers3

1

There are couple of way you can pass an object from one activity to another:

1. Application Class: this class is visible to all your application Activities so you can save your object in this class from one Activity and then access it from the other.

2. You can break apart your Class into the simple variables: string and 3 integers and pass them via a bundle or the intent it self from one activity to another, then construct your object again.

Intent intent = new Intent (this, TargetActivity.class);
intent.putExtra(KEY, value);
intent.putExtra(KEY, "value");
startActivity(intent);  

3. If your object implements Serializable/Parcelable then you can pass it via a bundle.

Example on how to serialize an object:

How do I serialize an object and save it to a file in Android?

Community
  • 1
  • 1
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • Thx :) but what is this "Serializable"? – LosTheAlef Apr 05 '13 at 13:20
  • 1
    Imagine trying to move a large piece of furniture through a door, but it's too big. Instead, you take it apart and move each piece through the door single file. Once inside, you reconstruct the piece of furniture. In essence, that is what Serializable allows you to do. You can take a complex object, deconstruct it to a simpler form, and then reconstruct back to the complex form (in this case after passing it via a Bundle). – codebaum Apr 05 '13 at 14:09
  • Thank you but some new problemes aroused with my class and the use of it. I prefer to change my whole planning. – LosTheAlef Apr 07 '13 at 17:24
0

One option could be implementing Serializable interface and then you can pass object instances in intent extra using putExtra(Serializable..) variant of the Intent#putExtra() method.

    //to pass :
   intent.putExtra("MyClass", obj);  

// to retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");
Sourabh Saldi
  • 3,567
  • 6
  • 34
  • 57
0

It can be tricky, because there's no guarantee that your application can't be killed between activities. Actually, it can be killed during activities, so keeping persistent objects around can be tricky.

My preferred way to do this is the "singleton pattern" in which you create a class whose purpose is to create a single instance that holds whatever data you want to hang around. If your application gets killed, the singleton instance will be lost and have to be re-created, but all Android apps run this risk all the time anyway.

See Save multiple instances states of the same Activity in Android for my implementation of a singleton in Android.

Oh, and I should add that this only works within an application where all the activities are in the same process, sharing the same address space. Otherwise, you'll have to make your object serialiazable and write it off to a file.

Community
  • 1
  • 1
Edward Falk
  • 9,991
  • 11
  • 77
  • 112