0

Currently I am able to pass an object called Exhibit to another Activity by putting it into putExtras and starting the intent. Now, what if I want to pass the object to another object?

For example I send like this:

public void onListItemClick(ListView parent, View v, int position, long id) { 
    Intent i = new Intent(this, ExhibitOpen.class);
    Bundle bundle = new Bundle();
    bundle.putSerializable("MyClass", (Serializable) exhibits.get(position));
    i.putExtras(bundle);
    startActivity(i);
}

Then I receive:

Intent i = getIntent();
dene = (Exhibit)i.getSerializableExtra("MyClass");

Here you can see that I am passing exhibits.get(position) to certain class and start the class as new activity, then the new activity receives it. So, how can I pass the object to another class (not this class) without starting it?

Thanks a lot!

Nurgul Alshyn
  • 79
  • 3
  • 11
  • Store the info somewhere and have the other class retrieve it later on. [Android Storage Options](http://developer.android.com/guide/topics/data/data-storage.html) – takendarkk Jul 24 '14 at 02:28
  • Seems like a good point. But how can I do it? Any suggestions? – Nurgul Alshyn Jul 24 '14 at 02:36
  • I gave you a link to all the android storage options. Just read through them to choose the one you want. – takendarkk Jul 24 '14 at 02:36
  • You should use Parcelable instead of Serializable in Extras, as it is way faster. Best answer is by @code-apprentice. Even better, you could define an interface for the communication between your Activity and the other class receiving the object. – momo Jul 24 '14 at 05:08

3 Answers3

2

If you don't want your object to be persisted during app launches, you can just set it as a static instance variable of your application class:

public class MyApp extends Application {
    private static Exhibit sExhibit;

    public static void setExhibit(Exhibit exhibit) {
        sExhibit = exhibit;
    }

    public static Exhibit getExhibit() {
        return sHexhibit;
    }
}

// To set the object:
MyApp.setExhibit(myExhibit);

// To retrieve it
Exhibit myExhibit = MyApp.getExhibit()

If you don't want to extend your application, you can just do it in any class, or in your Exhibit model, wherever it would make the more sense.

If you want it to persist in between app launches and it's serializable, I would use the Shared Preferences to store it: http://developer.android.com/guide/topics/data/data-storage.html#pref

nbarraille
  • 9,926
  • 14
  • 65
  • 92
  • yes agree with @nbarraille this is a best way to pass any dataobject without intent but be aware lots of static instance will be cause context leakage – Simmant Jul 24 '14 at 04:10
  • This is known as Singleton , You should restrict the creation of MyApp instance and define it in the same class. Currently that instance is null. – Viswanath Lekshmanan Jul 24 '14 at 04:14
1

You can just call a method on the object:

otherObject.someMethod(dene);
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

Please check this LINK1 , LINK2 and LINK3

Community
  • 1
  • 1
DJhon
  • 1,548
  • 3
  • 22
  • 39