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?