0

I am writing an alarm based app for Android, the app consist of 2 activities:

MainActivity - which the user can set the alarm parameters.

DataPage - which monitors the relevant data and display to the user, if data is out of limit, an alarm is triggered (using m.player).

The problem is, when the user navigates back from the DataPage, it returns to the MainActivity of course, but the DataPage process is still running on the background, this is a problem since if the user set the alarm again, a new DataPage is running and now there are 2 alarms set (without the option the cancel the first one).

So, how do I stop the activity and its Java class process on back button pressed?

I overrode the onBackPressed() method like this -

finish();

Intent intent = new Intent(this, DataPage.class);

stopService(intent);
Kartik Shandilya
  • 3,796
  • 5
  • 24
  • 42
Papino
  • 1
  • 2
  • you can set flag on starting the service of alarm, if flag is set do not trigger it again.. instead of flag then you can check if service is running or not if service is running then do not start the service again. AND to stop the alarm you can stop it calling the relative function of service and at the end of that function stop the service. – Arwy Shelke Jun 23 '19 at 07:54
  • i really think the right way is to clear totally the class and the activity, i would love if after pressing back button and going back to the mainActivity the app will be at the same position just like if the user just opened it (without any other services running in the background. many thanks! – Papino Jun 23 '19 at 08:10
  • Yes you can finish the activity the way you said but how can you stop or reset the alarm if that alarm is running in background service..??? For this you have to call a service function to stop alarm before of finishing the activity... – Arwy Shelke Jun 23 '19 at 08:30
  • Hello Papino, welcome to SO! We're going to need a little bit more of the puzzle pieces here to be able to help you; we need to see a bunch of things, but at the very minimum, how are you opening the DataActivity? What's the code in its onCreate and other methods. How does your "service of alarm" look like? Can you post some of its code? Are you using WorkManager? – Martin Marconcini Jun 23 '19 at 08:45
  • well, i use this: – Papino Jun 23 '19 at 09:06
  • well, i am opening the DataActivity with the following code (not sure how tomark it as code here...): 'startActivity(new Intent(com.example.gofreeanchoralarm.MainActivity.this, com.example.gofreeanchoralarm.DataPage.class));' – Papino Jun 23 '19 at 09:09
  • how do i put my code here in the comments? it limits my character, thanks – Papino Jun 23 '19 at 09:12

3 Answers3

0

There are many answers to this question. You should search first instead of directly posting it here.

Simple Override onBackPressed Method:

 @Override
public void onBackPressed() {
        super.onBackPressed();
        this.finish();
}
  • I tried this as mentioned in my post, it wont stop the alarm and the class is still running in the background.... the app going back to the MainActivity, but i can see on the "run" log that the class is still active... – Papino Jun 23 '19 at 08:09
  • If still this does not solve you can refer to a thread, where this type of problem is explained. https://stackoverflow.com/questions/4778754/how-do-i-kill-an-activity-when-the-back-button-is-pressed – Tanveer singh Bhatia Jun 23 '19 at 08:13
0

This may be helpful:

Add android:noHistory="true" attribute in that activity block in the manifest which you want to stop, this attribute will remove an activity from the stack whenever it is navigated away from using back pressed.

Source

Source says:

Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. The default value is "false".

A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it. In this case, onActivityResult() is never called if you start another activity for a result from this activity.

Community
  • 1
  • 1
FutureJJ
  • 2,368
  • 1
  • 19
  • 30
  • well, its important to say that the service most be running on the background at all times (so the alarm is on even when the user is doing other stuff on the phone), it should only be closed on back pressed. – Papino Jun 23 '19 at 09:12
0

Finishing of activity is not the problem, but the scheduled alarm is the problem, when you exit from the activity, scheduled alarm may be still active.

If you do not want to trigger the alarm if activity is not in focus, then you need to cancel the alarm. If you are using AlarmManager then call below method:

AlarmManager.cancel();
Gaurav Bansal
  • 604
  • 5
  • 11
  • The issue is less the alarm, i can shut is off by stoping it on the backButtonPressed override. – Papino Jun 23 '19 at 10:19
  • The problem is that the class is still running on the back, i can see its comments on the Run log. – Papino Jun 23 '19 at 10:20
  • @Papino Please tell me your code structure in more details, atleast tell what is the flow and what activity and service are u using ? – Gaurav Bansal Jun 23 '19 at 11:33