0

My client wants their app to always show a "WARNING" screen when the application starts or when it awakens from sleep. I've tried creating an onResume() event in my master activity (which every other activity inherits from), but this causes an endless loop:

  1. Activity is called, onResume() is fired
  2. Warning screen fires, causing the calling activity to be paused
  3. User clicks OK to accept the message, returning the user to the prior screen
  4. Activity is woken up
  5. Go to 1

Even if I could get around the endless loop, the Warning screen would fire whenever a new activity loads. This is what I like to call a Bad Thing.

Is there a way to mimic the onResume() event but at the application level rather than at the activity level, so that I can avoid these scenarios but still have the warning pop up on application wake?

Don Quixote
  • 235
  • 1
  • 5
  • 15

3 Answers3

3

Why not just use SharedPreferences.

http://android-er.blogspot.com/2011/01/example-of-using-sharedpreferencesedito.html

Store the time the popup is brought up, and if it was within 5 mins, or something, then don't pop it up.

This will break your loop and not completely annoy the user.

James Black
  • 41,583
  • 10
  • 86
  • 166
  • Thank you... I changed onResume to (1) retrieve the lastWarnedPref global into a variable (lastTime), (2) get the current time (currentTime), (3) write currentTime to the lastWarnedPref global (so it won't bug them as long as they're moving through activities), and (4) check the difference between currentTime and lastTime. I let it sit for a few minutes and then restarted the app and I got my warning message. I then immediately exited and restarted the app and got no warning message. It's not exactly what they want, but it may be what they get unless I can find something better. – Don Quixote Jun 26 '12 at 20:31
  • Oh, I also wound up adding an onDestroy override to clear the value of lastWarnedPref on exit, so that it will show the warning when they leave the app and return (which is what my client wanted). Thank you again, great answer. – Don Quixote Jun 26 '12 at 22:05
0

I would write a method to pop the warning and, in onPause, set a global flag. Check that global flag in the onResume, then reset it in your popup method. Simplified pseudo code...

class myApplication Extends Application{
    boolean appIsPaused = false;
}

class myActivity Extends Activity{

  @Override
  protected void onPause(){
      appIsPaused = true;
  }

  @Override
  protected void onPause(){
      if (appIsPaused){
          showPopup();
      }
  }

  public void showPopUp{

     if (!appIsPaused){
        return;
     }

     appIsPaused = false;

  }


}
Simon
  • 14,407
  • 8
  • 46
  • 61
  • Unfortunately that doesn't solve the issue of when they move from activity to activity. Each activity will cause its own onResume() to be fired, which will cause the warning to pop up every time they move around. – Don Quixote Jun 26 '12 at 20:32
0

You could use an AlertDialog to show the warning message, it would solve your problem.

Else, try to start the warning screen from the onStart or onRestart of your application ? Here's the android lifecycle if it can help : http://developer.android.com/reference/android/app/Activity.html

abecker
  • 885
  • 1
  • 10
  • 15