Question

into my application i use an intent:

private Handler mHandler = new Handler();  
.
.  
mServiceIntent = new Intent(this, ObdGatewayService.class);
mServiceConnection = new ObdGatewayServiceConnection();
mServiceConnection.setServiceListener(mListener);
// bind service
Log.d(TAG, "Binding service..");
bindService(mServiceIntent, mServiceConnection,
                Context.BIND_AUTO_CREATE);

here my activity at onCreate start a new service. this is my onDestroy:

    @Override
protected void onDestroy() {
    super.onDestroy();

    mServiceIntent = null;
    mServiceConnection = null;
    mListener = null;
    mHandler = null;
}

this is mServiceConnection:

public class ObdGatewayServiceConnection implements ServiceConnection{

private static final String TAG = "com.echodrive.io.ObdGatewayServiceConnection";

private IPostMonitor service = null;
private IPostListener listener = null;

public void onServiceConnected(ComponentName name, IBinder binder) {
    service = (IPostMonitor) binder;
    service.setListener(listener);
}


public void onServiceDisconnected(ComponentName name) {
    service = null;
    Log.d(TAG, "Service disconnesso.");
}


public boolean isRunning() {
    if (service == null) {
        return false;
    }

    return service.isRunning();
}


public void addJobToQueue(ObdCommandJob job) {
    if (null != service)
        service.addJobToQueue(job);
}

public void setServiceListener(IPostListener listener) {
    this.listener = listener;
}

mListener is a listener from interface:

public interface IPostListener {
void fineTest(DatiTest risultati);
void startAcquisizione();
void aquisizioneTerminata();
void aquisizioneInterrotta(String motivo);
void connessioneCorretta();
void gpsStato(boolean stato);
}

my problem is.. how save all this code after rotation? thanks!

Was it helpful?

Solution

The recommended way to save state across rotations is to save them on the outState. This is accomplished by overriding the onSaveInstanceState method. This method gives you a Bundle outState object that you can add Parcelable and Serializable objects to. This should work fine for your Intent object since it implements Parcelable but it may not work for say Handler because it only extends Object.

Another solution is to make these members static. However, be very careful if you decide to do this. Make sure that the value of the static member never holds on to a Context or a view hierarchy, etc, or you could easily introduce memory leaks.

If neither of these is acceptable to you, there is the option suggested by Tushar. However, unless you're careful this will make your life very difficult very fast. A large reason why activities are destroyed and re-created is so that resources can be re-loaded. So if you have layouts, strings, colors, dimens, or basically any resource specifically for landscape, or tablets, or different versions, you'll have to reload the entire UI yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top