user2386226
user2386226

Reputation:

Whats the Best approach to show an alert dialog every 5 minutes ,after a view /activity/frag is visible?

I am planning to implement a timer in my app such that only specific activities will have it, say I have 2 activities,1 fragment with this. When I move to activity 1, the timer starts and if I am still on the same view after 5 minutes, it should show an alert dialog asking to refresh the view. upon clicking ok it will refresh and restart the timer again(will start the countdown again) , upon clicking cancel, it will not refresh the page but restart the timer.

This should not happen if my activity or fragment is not in view. Say if I am on Activity 1, and the timer is at 2 mins, and I navigate to Activity 2, the timer goes to 0 and does not count in the background until the view is visible again.

Any ideas about whats the best approach to go about it? Any examples would help

Thanks in advance!

Upvotes: 0

Views: 584

Answers (1)

LalitaCode
LalitaCode

Reputation: 410

Create a class with timer methods, such as timerStart, timerStop. Use java timer Timer in the class. Schedule a timerTask(with required time) on timerStart method where you show a dialog after a certain time and cancel the timer on timerStop method.

Timer timer;

public void timerStart()
{
timer = new Timer();
timer.schedule(new timerTask()
{

@Override
public void run() {

{Show your dialog}

}


},delay);
}

public void timerStop()
{
  timer.cancel();
}

Instantiate the class in your first activity and call timerStart on the onCreate method of the activity.

Call timerStop and timerStart when the user clicks the cancel button on your dialog.

Refresh the page on clicking ok and call timerStop and timerStart again.

In the onPause method of your first activity call timerStop. This cancels the timer when you navigate to some other activity.

Upvotes: 0

Related Questions