You can use singleTon class mechanism here.
- Try to create a singleton class in your app.
- Get instance of the class in your activity.
- set the data to the class in
onCreate()
- get the data from the class whenever you want say in
onRestart()
.
To learn more about singleTon class see this..
Create singleton class
public class Singleton {
private static Singleton singleton = new Singleton();
/*
* A private Constructor prevents any other class from instantiating.
*/
public String valueToStore;
private Singleton() {
}
/* Static 'instance' method */
public static Singleton getInstance() {
return singleton;
}
public String getValueToStore() {
return valueToStore;
}
public void setValueToStore(String valueToStore) {
this.valueToStore = valueToStore;
}
}
In your activity.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Singleton singleton = Singleton.getInstance();
singleton.setValueToStore("Hai");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Singleton singleton = Singleton.getInstance();
Log.d("Value", singleton.getValueToStore());
}