Skip to content

Instantly share code, notes, and snippets.

@timfreiheit
Created July 28, 2015 09:58
Show Gist options
  • Save timfreiheit/ff8e2a6c51d09f882da8 to your computer and use it in GitHub Desktop.
Save timfreiheit/ff8e2a6c51d09f882da8 to your computer and use it in GitHub Desktop.
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import java.util.Vector;
/**
* Message Handler class that supports buffering up of messages when the
* activity is paused i.e. in the background.
*/
public abstract class PauseHandler extends Handler {
private static final String S_TAG = PauseHandler.class.getSimpleName();
public final String TAG = getClass().getSimpleName();
/**
* setUp PauseHandler for activity
* should run in onCreate
* @param activity activity to attach
* @param savedInstanceState savedInstanceState to check if recreation is needed
* @param newHandler handler to handle events (no used when savedInstanceState != null)
*/
public static void setUp(Activity activity,Bundle savedInstanceState, PauseHandler newHandler){
if(savedInstanceState != null){
return;
}
newHandler.setActivity(activity);
if(activity instanceof FragmentActivity){
final SupportState state = new SupportState();
state.setHandler(newHandler);
final android.support.v4.app.FragmentManager fm = ((FragmentActivity) activity).getSupportFragmentManager();
final android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.add(state, SupportState.TAG);
ft.commit();
}else{
final State state = new State();
state.setHandler(newHandler);
final FragmentManager fm = activity.getFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
ft.add(state, State.TAG);
ft.commit();
}
}
/**
* send Message to handler if exists
* @see android.os.Handler#obtainMessage(int)
*/
public static void sendMessage(Activity activity, int what){
PauseHandler handler = getPauseHandler(activity);
if(handler == null){
Log.e(S_TAG,"Handler not found");
return;
}
Log.d(S_TAG,"sendMessage");
handler.sendMessage(
handler.obtainMessage(what));
}
/**
* send Message to handler if exists
* @see android.os.Handler#obtainMessage(int, int, int)
*/
public static void sendMessage(Activity activity, int what, int arg1, int arg2){
PauseHandler handler = getPauseHandler(activity);
if(handler == null){
return;
}
handler.sendMessage(
handler.obtainMessage(what, arg1, arg2));
}
/**
* send Message to handler if exists
* @see android.os.Handler#obtainMessage(int, int, int, Object)
*/
public static void sendMessage(Activity activity, int what, int arg1, int arg2, Object obj){
PauseHandler handler = getPauseHandler(activity);
if(handler == null){
return;
}
handler.sendMessage(
handler.obtainMessage(what, arg1, arg2, obj));
}
/**
* send Message to handler if exists
* @see android.os.Handler#obtainMessage(int, Object)
*/
public static void sendMessage(Activity activity, int what, Object obj){
PauseHandler handler = getPauseHandler(activity);
if(handler == null){
return;
}
handler.sendMessage(
handler.obtainMessage(what, obj));
}
public static PauseHandler getPauseHandler(Activity activity){
if(activity instanceof FragmentActivity){
final android.support.v4.app.FragmentManager fm = ((FragmentActivity) activity).getSupportFragmentManager();
SupportState fragment = (SupportState) fm.findFragmentByTag(SupportState.TAG);
if (fragment != null) {
return fragment.handler;
}
}else {
final FragmentManager fm = activity.getFragmentManager();
State fragment = (State) fm.findFragmentByTag(State.TAG);
if (fragment != null) {
return fragment.handler;
}
}
return null;
}
/**
* Flag indicating the pause state
*/
private Activity activity;
/**
* Message Queue Buffer
*/
final Vector<Message> messageQueueBuffer = new Vector<Message>();
/**
* Flag indicating the pause state
*/
private boolean paused;
/**
* Resume the handler
*/
final public void resume() {
paused = false;
while (messageQueueBuffer.size() > 0) {
final Message msg = messageQueueBuffer.elementAt(0);
messageQueueBuffer.removeElementAt(0);
sendMessage(msg);
}
}
/**
* Pause the handler
*/
final public void pause() {
paused = true;
}
/**
* Notification that the message is about to be stored as the activity is
* paused. If not handled the message will be saved and replayed when the
* activity resumes.
*
* @param message
* the message which optional can be handled
* @return true if the message is to be stored
*/
protected boolean storeMessage(Message message){
return true;
}
/**
* Notification message to be processed. This will either be directly from
* handleMessage or played back from a saved message when the activity was
* paused.
*
* @param message
* the message to be handled
*/
protected abstract void processMessage(Message message);
public <T extends Activity> void setActivity(T activity){
this.activity = activity;
}
@SuppressWarnings("unchecked")
public <T extends Activity> T getActivity(){
return (T) activity;
}
/** {@inheritDoc} */
@Override
final public void handleMessage(Message msg) {
Log.d(TAG, "handleMessage");
if (paused) {
if (storeMessage(msg)) {
Message msgCopy = new Message();
msgCopy.copyFrom(msg);
messageQueueBuffer.add(msgCopy);
}
} else if(activity != null){
Log.d(TAG, "processMessage");
processMessage(msg);
}
}
public final static class State extends Fragment {
static final String TAG = "State";
/**
* Handler for this activity
*/
public PauseHandler handler;
public void setHandler(PauseHandler handler){
this.handler = handler;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public void onResume() {
super.onResume();
if(handler == null){
return;
}
handler.setActivity(getActivity());
handler.resume();
}
@Override
public void onPause() {
super.onPause();
if(handler == null){
return;
}
handler.pause();
}
public void onDestroy() {
super.onDestroy();
if(handler == null){
return;
}
handler.setActivity(null);
}
}
public final static class SupportState extends android.support.v4.app.Fragment {
static final String TAG = "SupportState";
/**
* Handler for this activity
*/
public PauseHandler handler;
public void setHandler(PauseHandler handler){
this.handler = handler;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public void onResume() {
super.onResume();
if(handler == null){
return;
}
handler.setActivity(getActivity());
handler.resume();
}
@Override
public void onPause() {
super.onPause();
if(handler == null){
return;
}
handler.pause();
}
public void onDestroy() {
super.onDestroy();
if(handler == null){
return;
}
handler.setActivity(null);
}
}
}
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
//setUpPauseHandler
PauseHandler.setUp(this,savedInstanceState,new CustomPauseHandler());
}
public void sendMessage(){
PauseHandler.sendMessage(this, 0);
}
static class CustomPauseHandler extends PauseHandler{
@Override
protected void processMessage(Message message) {
// handle message
Activity activity = getActivity();
activity.finish();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment