0

I am starting an ActivityB from ActivityA using startActivity(intent), and then I start a CountDownTimer. Now on timeout, I want to end ActivityB and have the ActivityA on foreground again. Is it possible to stop an Activity from another?

codeMagic
  • 44,549
  • 13
  • 77
  • 93
Code_Yoga
  • 2,968
  • 6
  • 30
  • 49
  • 1
    you can use a timer task, count down timer or a handler call `finish()` when required.. http://stackoverflow.com/questions/17839419/android-thread-for-a-timer/17839725#17839725 – Raghunandan Aug 10 '13 at 19:31
  • 1
    **"Is it possible to stop an Activity from another?"** - No. As soon as ActivityB enters a running state, ActivityA will be in either a paused or stopped state. In a rarer scenario when system resources are low, ActivityA might even be destroyed by the OS. As codeMagic suggests, the best approach would be have ActivityB self-terminate. In saying that, however, I can't think of what it is you're trying to achieve and why you'd want to do this. – Squonk Aug 10 '13 at 19:32

2 Answers2

6

On Activity B do this -

 // 30 seconds coundowntimer 
 new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {

     }

     public void onFinish() {
       finish() // finish ActivityB
     }
  }.start();

Don't call finish on ActivityA when you start ActivityB or make it singleInstance otherwise instead of finish() call startActivity(ActivityB.this,ActivityA.class) in onFinish().

mihirjoshi
  • 12,161
  • 7
  • 47
  • 78
2

Some code would help out tremendously to understand what you are doing. However, you could easily put the CoundtDownTimer in ActivityB and start it in its onCreate(). Then when it finishes just call finish() in ActivityB and as long as you haven't called finish() on ActivityA then you should get your desired result.

If this is not what you want then please supply some code and explain your issue a little better.

codeMagic
  • 44,549
  • 13
  • 77
  • 93