Last active
September 17, 2016 18:10
-
-
Save chrisjenx/5003825 to your computer and use it in GitHub Desktop.
QueuedUiRunnableFragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Intent; | |
import com.actionbarsherlock.app.SherlockFragment; | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
import java.util.List; | |
/** | |
* Created with Intellij with Android, BIZZBY product. | |
* This file is Exclusivly licenced under Apache Commons Licence 2.0 | |
* <p/> | |
* User: chris | |
* Date: 22/12/2012 | |
* Time: 16:11 | |
*/ | |
public class BaseFragment extends SherlockFragment | |
{ | |
// UI Runnables | |
private final List<Runnable> mUiRunnables = new LinkedList<Runnable>(); | |
private boolean mIsPaused = false; | |
// ============ | |
@Override | |
public void onResume() | |
{ | |
super.onResume(); | |
mIsPaused = false; | |
runQueuedUiRunnables(); | |
} | |
@Override | |
public void onPause() | |
{ | |
mIsPaused = true; | |
super.onPause(); | |
} | |
/** | |
* Is the fragment paused? | |
* | |
* @return | |
*/ | |
public boolean isPaused() | |
{ | |
return mIsPaused; | |
} | |
/** | |
* Add a runnable task that can only be run during the activity being alive, things like dismissing dialogs when a background | |
* task completes when the user is away from the activity. | |
* | |
* @param runnable runnable to run during the ui being alive. | |
*/ | |
protected void postUiRunnable(final Runnable runnable) | |
{ | |
QLog.v("UiRunnables = " + runnable); | |
if (null == runnable){ | |
return; | |
} | |
if (!mIsPaused && BaseActivity.isUiThread()) | |
{ | |
runnable.run(); | |
} | |
else if (!mIsPaused && !BaseActivity.isUiThread() && getActivity() != null) | |
{ | |
getActivity().runOnUiThread(runnable); | |
} | |
else | |
{ | |
mUiRunnables.add(runnable); | |
} | |
} | |
/** | |
* Will run any pending UiRunnables on resuming the activity | |
*/ | |
private void runQueuedUiRunnables() | |
{ | |
if (mIsPaused) return; | |
if (mUiRunnables.isEmpty()) return; | |
QLog.d("UiRunnables Running"); | |
final Iterator<Runnable> it = mUiRunnables.iterator(); | |
Runnable run; | |
while (it.hasNext()) | |
{ | |
run = it.next(); | |
run.run(); | |
it.remove(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment