Skip to content

Commit

Permalink
Merge pull request #4836 from pingwang2011/timob-15324
Browse files Browse the repository at this point in the history
timob-15324:Android: expose the status of the current activity (foreground or background) to the module developers
  • Loading branch information
hieupham007 committed Oct 28, 2013
2 parents 270cf54 + a8664da commit 54a89ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,19 @@ public boolean activityStackHasLaunchActivity()
return false;
}

/**
* Check whether the current activity is in foreground or not.
* @return true if the current activity is in foreground; false otherwise.
* @module.api
*/
public static boolean isCurrentActivityInForeground()
{
Activity currentActivity = getAppCurrentActivity();
if (currentActivity instanceof TiBaseActivity) {
return ((TiBaseActivity)currentActivity).isInForeground();
}
return false;
}

/**
* This is a convenience method to avoid having to check TiApplication.getInstance() is not null every
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public abstract class TiBaseActivity extends FragmentActivity

private boolean onDestroyFired = false;
private int originalOrientationMode = -1;
private boolean inForeground = false; // Indicates whether this activity is in foreground or not.
private TiWeakList<OnLifecycleEvent> lifecycleListeners = new TiWeakList<OnLifecycleEvent>();
private TiWeakList<OnWindowFocusChangedEvent> windowFocusChangedListeners = new TiWeakList<OnWindowFocusChangedEvent>();
private TiWeakList<interceptOnBackPressedEvent> interceptOnBackPressedListeners = new TiWeakList<interceptOnBackPressedEvent>();
Expand Down Expand Up @@ -461,6 +462,7 @@ protected void onCreate(Bundle savedInstanceState)
{
Log.d(TAG, "Activity " + this + " onCreate", Log.DEBUG_MODE);

inForeground = true;
TiApplication tiApp = getTiApp();

if (tiApp.isRestartPending()) {
Expand Down Expand Up @@ -552,6 +554,11 @@ public int getOriginalOrientationMode()
return originalOrientationMode;
}

public boolean isInForeground()
{
return inForeground;
}

protected void sendMessage(final int msgId)
{
if (messenger == null || msgId == -1) {
Expand Down Expand Up @@ -925,6 +932,7 @@ public void onWindowFocusChanged(boolean hasFocus)
*/
protected void onPause()
{
inForeground = false;
super.onPause();
isResumed = false;

Expand Down Expand Up @@ -982,6 +990,7 @@ protected void onPause()
*/
protected void onResume()
{
inForeground = true;
super.onResume();
if (isFinishing()) {
return;
Expand Down Expand Up @@ -1035,6 +1044,7 @@ protected void onResume()
*/
protected void onStart()
{
inForeground = true;
super.onStart();
if (isFinishing()) {
return;
Expand Down Expand Up @@ -1093,6 +1103,7 @@ protected void onStart()
*/
protected void onStop()
{
inForeground = false;
super.onStop();

Log.d(TAG, "Activity " + this + " onStop", Log.DEBUG_MODE);
Expand Down Expand Up @@ -1128,6 +1139,7 @@ protected void onStop()
*/
protected void onRestart()
{
inForeground = true;
super.onRestart();

Log.d(TAG, "Activity " + this + " onRestart", Log.DEBUG_MODE);
Expand Down Expand Up @@ -1187,6 +1199,7 @@ protected void onDestroy()
{
Log.d(TAG, "Activity " + this + " onDestroy", Log.DEBUG_MODE);

inForeground = false;
TiApplication tiApp = getTiApp();
//Clean up dialogs when activity is destroyed.
releaseDialogs(true);
Expand Down

0 comments on commit 54a89ba

Please sign in to comment.