Skip to content

Instantly share code, notes, and snippets.

@milosmns
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milosmns/0df33f49be9e44698af9 to your computer and use it in GitHub Desktop.
Save milosmns/0df33f49be9e44698af9 to your computer and use it in GitHub Desktop.
Hack for the Activity Lifecycle annoyance
package me.angrybyte.lifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
/**
* <p>
* A simple, <i>hacky</i> solution to Android's Activity Lifecycle which has no guaranteed invocation of {@link Activity#onStop()} and
* {@link Activity#onDestroy()} methods. Extending this class in your activity should guarantee invocation of these methods just before
* invoking the {@link Activity#onCreate(Bundle)} or {@link Activity#onCreate(Bundle, PersistableBundle)} on your new Activity instance.
* </p>
* <p>
* The main difference is that you will need to implement (instead of regular {@code stop} and {@code destroy} methods) two new methods
* which will do that work for you - {@link AbstractLifecycleActivity#doOnStopOperation()} and
* {@link AbstractLifecycleActivity#doOnDestroyOperation()}.
* </p>
* <i>Note that you still need to call {@code super} methods on each of the activity lifecycle events.</i><br>
* <i>Note that {@code stop} and {@code destroy} events will not happen twice, only the first time it happens.</i>
*/
public abstract class AbstractLifecycleActivity extends AppCompatActivity {
public boolean stopInvoked;
public boolean destroyInvoked;
protected static AbstractLifecycleActivity lastInstance;
/**
* Invoked when {@link Activity#onStop()} event happens <b>on this instance</b> of the activity.
*/
public abstract void doOnStopOperation();
/**
* Invoked when {@link Activity#onDestroy()} event happens <b>on this instance</b> of the activity.
*/
public abstract void doOnDestroyOperation();
/**
* This method checks if the current instance is the same one as the one before, and calls {@code stop} and {@code destroy} methods if
* they haven't been called yet. After this, these events will not be called again through this class.
*/
private void invokeLifecycleOperations() {
if (lastInstance != null && lastInstance != this) {
if (!lastInstance.stopInvoked) {
lastInstance.doOnStopOperation();
lastInstance.stopInvoked = true;
}
if (!lastInstance.destroyInvoked) {
lastInstance.doOnDestroyOperation();
lastInstance.destroyInvoked = true;
}
}
lastInstance = this;
}
/**
* @inheritDoc
*/
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
invokeLifecycleOperations();
super.onCreate(savedInstanceState, persistentState);
}
/**
* @inheritDoc
*/
@Override
public void onCreate(Bundle savedInstanceState) {
invokeLifecycleOperations();
super.onCreate(savedInstanceState);
}
/**
* This method calls {@link Activity#onStop()} on the parent class after {@link #doOnStopOperation()} is finished.
*/
@Override
protected void onStop() {
if (!stopInvoked) {
doOnStopOperation();
stopInvoked = true;
}
super.onStop();
}
/**
* This method calls {@link Activity#onDestroy()} on the parent class after {@link #doOnDestroyOperation()} is finished.
*/
@Override
protected void onDestroy() {
if (!destroyInvoked) {
doOnDestroyOperation();
destroyInvoked = true;
}
super.onDestroy();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment