Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you make the app not reload when the user clicks on a notification?

 
Eric Sweeten
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want the app to continue where it left off when the user minimized when when the user clicks on the notification. Right now, it seems to reload the app from scratch.

It performs a background task and a notification pops up when it's done, but it's pointless because it just reloads the whole app.

This is the code for the notification, but I'm guessing it's something else. Maybe some simple setting on not exiting on minimize?



Any ideas?
 
Tim Moores
Bartender
Posts: 3137
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Android apps generally have little control over that. You should assume that an app gets terminated soon after it is no longer in the foreground. That will not happen always, but you need to assume that it will.

That means the onPause method should save all information the app needs to restore its state when it next gets started. The javadoc of the Activity class has a lot of information about the lifecycle, and which of its methods you should override to make it a seamless user experience.
 
Eric Sweeten
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:Android apps generally have little control over that. You should assume that an app gets terminated soon after it is no longer in the foreground. That will not happen always, but you need to assume that it will.

That means the onPause method should save all information the app needs to restore its state when it next gets started. The javadoc of the Activity class has a lot of information about the lifecycle, and which of its methods you should override to make it a seamless user experience.


That bites, because my app is a number cruncher which, when you run numbers, can take a while to calculate. I wish there was a way for it to run in the background while the user of the phone can do other things on the phone.

It's a lottery app, and you can run numbers "until jackpot", which plays virtual day-after-day until you hit a jackpot and can take hours or longer to compute. So this would be a really great feature for the phone to have. It's unfortunate if the app can't run in the background. So you're saying there's nothing I can do about this?
 
Tim Moores
Bartender
Posts: 3137
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way to run background operations that have no UI is to use a Service, not an Activity: http://developer.android.com/guide/components/services.html
 
Eric Sweeten
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:The way to run background operations that have no UI is to use a Service, not an Activity: http://developer.android.com/guide/components/services.html


I read up on Services. Now I'm not sure what to do, or if there is even a solution. Since the service runs separate from the program, when the program exits, the service will be running, and then there's no way to reference that service when the program reloads. So I would assume to make a file and set a variable to let the program know the calculations are still being processed. But then that won't account for the phone being shut off or the service otherwise stopping unusually. If that happens, the app will still show that it's calculating a report when there is actually no service at all running. Is there a way to reference the service when the app reloads or am I right that there's not? If there's not, does that mean I don't have any real options?
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there's no way to reference that service when the program reloads

Not so. Activity can start the service, and later bind to that Service every time it resumes and comes to foreground. It can find out current status of whatever the Service is doing (in your case, the status of calculations) via the binding interface.

See http://developer.android.com/guide/components/services.html#CreatingBoundService and http://developer.android.com/guide/components/bound-services.html

Unrelated to that is the possibility that the Service itself may be terminated by platform under low memory conditions. So you have to do a couple of things:
- startService with START_STICKY, so that if Service is terminated, platform will do a best effort attempt at restarting it at some later point of time.
- Design the Service to anticipate unexpected termination. Basically, save application state somewhere periodically and resume from that point on after restart.
 
Tim Moores
Bartender
Posts: 3137
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're imagining more difficulties than there are, or that there are no ways around them.

Various app components can communicate using Intents, irrespective of whether one of them was terminated in the meantime.

Status can be kept in shared preferences; I think that would be preferable to files or a DB.

I'm sure services and/or activities can be started at boot/reboot time (not sure how, I've never looked into that).

Services have lifecycle methods for handling shutdowns in whichever way you need to.

And so on. If you haven't used services before, maybe start with a small app that tests these points in principle. It may all be easier than you suspect.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic