0

I'm new to Android development and I have some trouble understanding how to handle plain old java objects / variables / containers in Android. They seem to be not longer available after finishing an AsyncTask or leaving an Activity.

For e.g.: Via AsyncTask I'm gettings some data, push it into an ArrayList from another class (works pretty fine - I can see the result on my mapview ;-)) and want to make it available for other classes/Activities.

How can I handle this / what is a good practice in Android for that matter?

Thank you in advance.

Kody
  • 1,154
  • 3
  • 14
  • 31

2 Answers2

0

See here for ways to store data http://developer.android.com/guide/topics/data/data-storage.html

This first answer shows an example of how to pass data between activities How do I pass data between Activities in Android application?

Community
  • 1
  • 1
Sababado
  • 2,524
  • 5
  • 33
  • 51
0

How can I handle this / what is a good practice in Android for that matter?

If you want to pass primitive types and objects through Activities you have a few options:

  • An usage of putExtra() methods of Intent - you can pass primitive datatypes and objects as well

  • Wrap primitive datatypes and objects into Bundle and pass to Intent

But if you want to pass objects via Activities they have to implement Serializable or Parcelable interface.

They seem to be not longer available after finishing an AsyncTask or leaving an Activity.

Yes. If you have variables, objects defined and initialised in Activity class they live as long as Activity lives. So if you want to persist some data you are once again a few options:

  • You can persist primitive and reference types(int, double, String etc.) with SharedPreferences they work as key-value pairs.

  • For storing objects you can create their "copy" in SQLite where objects are presented as tables where properties of object are equal to columns in database.

  • If your objects implement Serializable interface, you can serialize them into binary file and whenever you want deserialize them for an usage.
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • Thank's for that helpful and detailed explanation :-) I guess I will solve this via SQLite, which seems to be solution that implies extensibility in persistence matters. – Kody Apr 01 '13 at 13:20