iOS AppDelegate equivalent in Android
You have to create your own subclass of Application and specify it in the AndroidManifest.xml. After that the new instance of this class will be created and can be accessed throughout your application.
Example:
Manifest.xml
<application name="YourApp">
YourApp.java
public class YourApp extends Application {
private String yourState;
public void setState(String state){
yourState = state;
}
public String getState(){
return yourState;
}
}
YourActivity.java
public class YourActivity extends Activity {
@Override
public void onCreate(Bundle bundle){
YourApp appState = ((YourApp)getApplicationContext());// you can use getApplication() as well in the activity
String state = appState.getState();
}
}
More info about Application
Make your main class implement ProcessLifecycleObserver
.
It is the equivalent of the AppDelegate in iOS as it "summarizes" activity handlers like onCreate
, onResume
on an app-process basis.
https://developer.android.com/reference/android/arch/lifecycle/ProcessLifecycleOwner
You should be creating a model object (your own class) to retain the data on both iOS and android. imho.