I have created a class
public class MyCountDownTimer extends CountDownTimer {
Activity activity;
public MyCountDownTimer(long startTime, long interval,Activity m_activity) {
super(startTime, interval);
activity = m_activity;
}
@Override
public void onFinish() {
//DO WHATEVER YOU WANT HERE
FunctionsUtil.ShowMessage("TERMINO SU TIEMPO",Toast.LENGTH_LONG);
valoresGenerales.banderaCountDownTimer = false;
android.os.Process.killProcess(android.os.Process.myPid());
}
@Override
public void onTick(long millisUntilFinished) {
if( millisUntilFinished/1000 == 35)
{
FunctionsUtil.ShowMessage("SI no usa en " + millisUntilFinished/1000 + " segundos se cerrara automáticamente.",Toast.LENGTH_LONG);
}
if( millisUntilFinished/1000 == 10)
{
FunctionsUtil.ShowMessage("SI no usa en " + millisUntilFinished/1000 + " segundos se cerrara automáticamente.",Toast.LENGTH_LONG);
}
}
}
as you know MyCountDownTimer... it's a backwards counter... what I'm trying to do is... close my app when this counter reaches 0 and enter the onFinish() I put the code
android.os.Process.killProcess(android.os.Process.myPid());
but doing tests I realized that it only closes a single activity (the one that is visible) and opens the actMain (main) automatically.
but my app has several activities that open (activity 1 opens activity2 which in turn opens activity 3 etc) that means that I have more than one activity in use and therefore the code does not help me.
what code could help me???
Thanks a lot.
I give you a solution in which your activities have to extend from a "parent" activity, for example, we suppose to have these 3 Activities, the 3 must extend from the parent activity
ParentActivity
:This would be the activity code
ParentActivity
, which has 2 methodssetActivity()
to add the activity reference to aList
andquitApp()
that searches for all the activities that were saved in the list and terminates them:Inside your activities you have to add the line:
to add the activity to the
List
.and in any you can call the method:
with which it will close all the
Activities
registered in theList
one defined inParentActivity
.In your code you could call
quitApp()
insideonFinish()
to close all Activities:When the timer runs out, have the main activity load on top of all the others.
and in your main activity in
onCreate
It was easy to do it, with this code
where act.. is the current activity (any screen)
and the
It is for my case the login screen (main screen)
It may be late to reply, but in case someone else has the same question.
All you have to do is put the method
For example, if you have a button to close the entire app, you could do something like this: