I am using the Android library Retrofit for networking in my app. The library calls for creating a RestAdapter for making service calls. I want to use this same instance of the RestAdapter for all of my service calls. How is my setup for this scenario?
Extending Application:
public class CustomApplication extends Application {
private RestClient restClient = null;
public RestClient getRestClient() {
return restClient;
}
public void initRestClient() {
if (restClient == null) {
restClient = new RestAdapter.Builder().setEndpoint(BASE_URL).build().create(RestClient.class);
}
}
}
Initializing instance and making sure I have access to the instance in all my Activities, without explicitly having to get it for each Activity:
public class BaseActivity extends Activity {
protected RestClient restClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((CustomApplication) this.getApplication()).initRestClient();
restClient = ((CustomApplication) this.getApplication()).getRestClient();
}
}
Then all my Activities will extend BaseActivity.