Please tell me how to save the Service , or rather the link to the Service and attached from the main Activity to the Service Callback ? I have to do this because when the orientation of the screen changes, the Activity is created anew and, of course, all variables are reset and the link to my Service is :(
Actually here is the fragment responsible for the Service in the Activity :
Intent intent; @Override protected void onStart() { super.onStart(); intent = new Intent(this, Sservice.class); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); } @Override protected void onStop() { super.onStop(); if (mBound) { unbindService(mConnection); mBound = false; } } private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { Sservice.LocalBinder binder = (Sservice.LocalBinder) service; mService = binder.getService(); mBound = true; mService.setReciever(response); } @Override public void onServiceDisconnected(ComponentName arg0) { mBound = false; } }; And this is my code responsible for storing data in the Bundle for later retrieval at the end of the screen rotation.
@Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); boolean saved = savedInstanceState.getBoolean("connected"); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putBoolean("connected",mService.connected); //savedInstanceState. Вот тут я хотел запихнуть в Bundle ссылку на сервис super.onSaveInstanceState(savedInstanceState); } Here is my Callback working from the service (surprisingly working):
Sservice.ResponseCallback response; @Override protected void onCreate(Bundle savedInstanceState) { instance = this; super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); response = new Sservice.ResponseCallback() { public void response(String response) { Log.i("Ура ура","Ok"); } }; }