0

I tried from this example https://stackoverflow.com/a/9472019/485978

I have a service, this service just connects to the database directly and I put all data in a bean which is located inside this class.

public class ServiceApplication extends Application {

  private static ServiceApplication mInstance;

  public ServiceApplication() {
    super();
  }

  public static ServiceApplication getInstance() {
    return mInstance;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    mInstance = this;
  }

  private Person personalData;

  public Person getPersonalData() {
    return personalData;
  }

  public void setPersonalData(Person personalData) {
    this.personalData = personalData;
  }
}

When retrieving a data from the database I used an AsyncTask where in doBackground() this is the code

ServiceApplication.getInstance().setPersonalData(personalData);
Log.d("AndroidService", "First name: "+ ServiceApplication.getInstance().getPersonalData().getFirstName());

So far it can retrieved the First Name.

However when I try to access those data from another activity all I get is null.

I tried two ways but it produces null

First implementation:

ServiceApplication app = (ServiceApplication) getApplication();
String name = (app.getPersonalData() != null) ? app.getPersonalData().getFirstName().trim() : "user";

Second implementation:

String name = (ServiceApplication.getInstance().getPersonalData() != null) ? ServiceApplication.getInstance().getPersonalData().getFirstName().trim() : "user";

Do you guys no how to persist the data and how to retrieve it from other activities?

Community
  • 1
  • 1
jantox
  • 2,185
  • 4
  • 21
  • 22
  • 1
    Your question seems vague to me, and your subclass of Application seems unneeded, google docs "There is normally no need to subclass Application." The fact that you have a database means you can access the data via it, if you want to be sending data to other Activities from the service you may want to stuff it in a bundle and Broadcast it out, then other activities can ReceiveBroadcast and unbundle the data. If you have complex data you need to broadcast you can subclass Parcelable and then send that. – esse Feb 28 '13 at 03:37
  • I just want to have a persistence data that does not in anyway invoke the database again and again. For example. If i login, I want to put in Person bean some personal info of the user, that way I can just use Person.getName() in other activities without the need to query again the db for these values. – jantox Feb 28 '13 at 05:02
  • Then if all of your Activities are within the same Application then your Singleton should work, just make sure all of its member variables are static too. Also, I don't think you need to extend Application. – esse Feb 28 '13 at 05:24
  • Problems arise because I am instantiating the singleton in AsyncTask, eventhough I synchronized it already. – jantox Mar 01 '13 at 03:07

2 Answers2

0

Note that only mInstance is static. It does not guarantee that you will receive the same personalData object. personalData could be and is definitely null in your case.

You might want to declare personalData as static as well.

private static Person personalData;

But in this case, you will reset the data on your next call to setPersonalData.

I would recommend you create a static structure as

private static ArrayList<Person> pList = new ArrayList<Person>();

Then in your setPersonalData() you can add objects to your pList.

You can replace your getPersonalData to return the pList. Hence you can use the same singleton instance from you activity to add data to the list and use the same to retrieve

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Robin Chander
  • 7,225
  • 3
  • 28
  • 42
0

If you are trying to pass data between a service and an activity you have to use another approach, as described here. If you just want to load Person data from a database into a singleton then you don't need to a Service. Just open a connection to the db, read the data and store it. Something like:

...
public Person getPersonalData() {
  if(personalData == null) {
    ... open the db and load the data here ...
  }

  return personalData;
}
...
ebarrenechea
  • 3,775
  • 1
  • 31
  • 37