Need to really close app when home is pressed

Creaky

Member
Licensed User
Longtime User
Hi all,

My app has a webview displaying a large animated gif file.
This consumes quite some CPU power, but that is not a problem when the user is looking at the animation.

When the user presses the homebutton though, the webview continues to play the animation in the background. The taskmanager on my SGSII shows the app even consumes up to 45% CPU time while in the background.
If the user doesn't notice this and quite the app with the exit menu item, it will suck the battery dry in less than an hour. So I really need a way to either kill my app when it is pushed to the background, or a way to have the webview stop running the animated gif.

Any suggestions?
 

Creaky

Member
Licensed User
Longtime User
Yes but you can't capture the home button key event to actually execute an ExitApplication...
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Yes but you can't capture the home button key event to actually execute an ExitApplication...

I thought the user quits with exit menu item, as mentioned in your post.

Anyway NJDude's solution should work
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
@NJDude Yeah but the trouble starts when the user doesn't manually close the app, but presses the home button.

Loading an empty html in the webview on pause and reloading the animated gif on resume doesn't seem the help. The app still keeps using about 45% CPU in the background... weird... When resuming I do see the webview reloading the animated gif. So maybe the webview is not the CPU hog. The question that remains then is, what does hog the cpu?
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
I tried the Activity.finish in the pause event, but that event is also triggered when going from portrait to landscape orientation or the other way around. So it would make the app close as soon as you change orientation...
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
@Erel I already tried that but it doesn't seem to help.
It might not even be the webview that is causing the problem, but since it is a very basic app, I have no clue what else to look for...
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
How about calling ...

B4X:
Sub Activity_Pause (UserClosed As Boolean)

   ExitApplication
        ...

That works fine for me.

Rolf
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Bad side effect!
(;-)
I use it in an app that is restricted to landscape view, so I never noticed.

Rolf
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Bad side effect!
(;-)
I use it in an app that is restricted to landscape view, so I never noticed.

Actually, it is possible (at least on SOME devices; not known if it's all) to get a rotation restart even if your app is restricted to landscape view, as noted in the Documentation Wiki:

1. While in your app, the device goes into Sleep mode, either through a time-out or by the user pressing the device's Sleep (power) button.

2. The user presses the power button to bring the device out of Sleep mode to the security/start-up screen and rotates that screen.

3. Your app will come back up in the supported orientation, but Android will have started it over as if the app itself had been rotated.
 
Upvote 0

CSP

Member
Licensed User
Longtime User
Solutions?

I am also having this same problem. Is there any way to know when the activity is no longer in focus?
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
Since I stopped using an animated gif in my app I no longer experience the CPU hogging problem. But I'm still not sure what caused the problem, as I tried every solution proposed in the thread without a positive result.

Please feel free to keep using this thread to address CPU hogging problems for apps in the background.
 
Upvote 0

CSP

Member
Licensed User
Longtime User
gifdecoder

I'm not sure if it's stated anywhere, it probably is, but I've found that if you don't call gif.DisposeFrames on application pause, then when on resume it will cause an error when loading the gif again and might also somehow be a part of your resource problem.
 
Upvote 0
Top