13

My application is a timer with alarm type app. When the timer expires i would like to bring the app's main activity back into view, if the user has navigated away, but the app is still running.

I show where i would like to place the necessary code:

   public class MyCount extends CountDownTimer {
        public MyCount(long millisInFuture, long countDownInterval) {
          super(millisInFuture, countDownInterval);
        }

        public void onFinish() {

         /** code here to bring the app back to the front / top of stack, in its current state */


         timeDisplay.setText("Expired.!");
         ReminderFlashingScreen.setVisibility(View.GONE);
         statustab.setBackgroundResource(R.drawable.redtab);
         seeker.setProgress(0);
         reset.setBackgroundResource(R.drawable.resetbtnoff);
         playExpiredAlarm(); 
         alarmAnimation.start();
         flasher.setVisibility(View.VISIBLE);
         StopAlarmButtonAnimation.start();
         StopAlarmButton.setVisibility(View.VISIBLE);
         expired = true;
        }

        public void onTick(long millisUntilFinished) {
            remindertimepref = prefs.getString("reminderTime", "<unset>");
            remindtime = Integer.parseInt(remindertimepref.trim());
            timeDisplay.setText(formatTime(millisUntilFinished));
            seeker.setProgress((int) (millisUntilFinished / 1000 / 60 ) + 1);
            if (used == true){
                reminder_time = ((int) (millisUntilFinished / 1000));
                if (reminder_time == remindtime){
                    reminderalarm();
                    used = false;
                    }
                }

          }
    }
11Monkeys
  • 145
  • 1
  • 1
  • 10
  • 1
    What if you user navigates away to something more memory intensive and the system decides to kill your app? My point is, I think you want to put your Timer into a service. Then you can send a broadcast which your app is listening for when the timer is done. Simpler & cleaner! – codinguser Dec 11 '10 at 11:16
  • 1
    Something i need to read up on.. New to android, and my first thought are: If the app is killed, whats keeping the count..? I know how to save current state, but serves no purpose as the timer would be paused. You solution sounds like the route i should be exploring, any pointers at what i should be searching for / looking into, appreciated. – 11Monkeys Dec 11 '10 at 11:27
  • 1
    Think about using the android AlarmManager http://developer.android.com/intl/de/reference/android/app/AlarmManager.html instead of keeping track of the time yourself at least use it after you receive the onPause call. This will save the user a lot of battery and CPU usage. – Janusz Dec 11 '10 at 12:56
  • Thanks guys... Your input gives me avenues to explore. – 11Monkeys Dec 11 '10 at 13:33
  • @codinguser if the "system decides to kill your app", then afaik your service is killed too. That is why @Janusz's suggestion of using the `AlarmManager` is a better suggestion. Even if the app gets killed it will still be able to be relaunched. – Richard Le Mesurier Apr 24 '13 at 14:47
  • @RichardLeMesurier You're right. `AlarmManager` is definitely the way to go. Although Android does restart (sticky) Services after the memory crisis is over. – codinguser Apr 25 '13 at 08:29
  • @codinguser - True. And your method actually will result in a much better behaved app - the `AlarmManager` is too aggressive for most use cases. Nice point. – Richard Le Mesurier Apr 25 '13 at 08:36

3 Answers3

5

For me it was this:

Intent intent = new Intent(Application.getContext(), AbstractFragmentActivity.instance.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Application.getContext().startActivity(intent);
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
Kevin Parker
  • 16,975
  • 20
  • 76
  • 105
0

You should put your timer in a Service, that way you're assured that Android won't kill your application while it's in background. (You can put your whole MyCount class in a service).

From the service you can always bring your application (Activity) to the front:

Intent intent = new Intent(getBaseContext(), TimerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(intent);
Akos
  • 82
  • 3
0

See http://developer.android.com/guide/appendix/faq/framework.html#4:

How can I check if an Activity is already running before starting it?

The general mechanism to start a new activity if its not running— or to bring the activity stack to the front if is already running in the background— is the to use the NEW_TASK_LAUNCH flag in the startActivity() call.

petteri
  • 354
  • 2
  • 7
  • It should happen as described above, but its still not working as it should be. I start my application, then tap home button, then app goes to background but still running, after 2-3 minutes i fire an intent with NEW_TASK_LAUNCH flag, but it starts application again from main entry point, not from where it left – Zoombie Aug 28 '12 at 05:44