1
votes

I want to stop my timer once my activity closes,,

Right now I create an activity that checks a service every 20 seconds for if there is an update. If there is it will send a notification with the same activity, if it gets clicked on it will open the same activity(So the one with the timer)

I tried using:

resultIntent.SetFlags (ActivityFlags.SingleTop);
[Activity (LaunchMode = LaunchMode.SingleTop, NoHistory = true) ] // Creates the Launchmode single top and set the nohistory true in the androidmanifest

But that did still create a new activity, with a new timer and a new oncreate ( Giving a variable that tells me in the oncreate if it's the first or not won't work because the timerevent is also checking if activity is paused or not, what I can't do if the timer event is in another activity than the open one)

So I couldn't get it working that it would open the current active activity instead of a new one, but now I want to look into stopping the other(Actually same class) activity's timer, so it won't trigger the notification if there's a newer one up.

1
uset launchmode singlInstance in manifest file - Biraj Zalavadia
Tried that, it did not work. - Onno
The best way to do the check of update is to programm an Alarm every x seconds, and then you don't need to set any timer or to worry about the activity Close because the AlarmManager is still launched in the background even if the Activity is closed - Houcine

1 Answers

0
votes

Try this param it should solve your problem:

<activity android:name="com.example.MyActivity"
                  android:label="@string/app_name"
                  android:launchMode="singleTask">

About timer:

timer.cancel();
timer.purge();