0

My app contains Activity. I start it by Intent. Than I turn my Activity to background (switch to Home Screen or similar). After some time the OS destroy my Activity (I think when memory is low). It is possible protect my Activity (in background) from destroy by OS? Or increase it priority to maximum?

Tapa Save
  • 4,769
  • 5
  • 32
  • 54
  • Never assume that an activity will persist after you close the application. You need to look into [Services](http://developer.android.com/reference/android/app/Service.html) for doing background tasks. – Andrew Schuster Nov 06 '13 at 18:11
  • My app also contains Service and this Service is always run in memory. I need reload data periodically inside Activity when it is in "invisible mode". But OS (may bee garbage collector) periodically destroy my Activity. I need protect my Activity from destroying. – Tapa Save Nov 06 '13 at 18:15
  • WHY is this question downvoted?! Isn't clear what he says, doesn't met forum rules?! - upvoting to fix that stupid action –  Nov 06 '13 at 18:21

2 Answers2

1

Activity is made for a UI holding elements. Service is made for background tasks. From this already sounds logic to make a Service instead of try to keep Activity in background.

Also there are BroadcastReceivers, which will be called on different actions. Boot completed, Internet lost, got and so on.

I think you know what do you want to archive, I would suggest telling in detail an will get a lot better response, with enumeration, maybe code part of component needed to use.

Keeping Activity forced alive is a bad idea in general.

  • Thanks, my App already contains Services, BroadCastReceivers. But I need keeping Activity for all time during my app (Service) is live. I can't reload data in Service, because I need Reload data in WebView with JS injections inside. – Tapa Save Nov 06 '13 at 18:19
  • I see... you should make a plugin for you phonegap like application and move logic to Java side instead if Javascript. If you search a bit over the net you can find solution for "how to call Java code from Javascript" - do a search now! Than move that code to Service and back to activity when needed –  Nov 06 '13 at 18:20
  • I don't use Phonegap. Only Activity, WebView inside Activity, JavaScript injection inside WebView. And I need keep the Activity always live while my Service don't stop then. – Tapa Save Nov 06 '13 at 18:24
  • 2
    That is a very bad design: consume memory when even not needed. if Activity isn't visible than should be killed , freed by system if he needs memory –  Nov 06 '13 at 18:25
  • Check this :http://android-er.blogspot.ro/2011/10/run-android-java-code-from-webpage.html and http://stackoverflow.com/questions/10389572/call-java-function-from-javascript-over-android-webview and this: http://ganeshtiwaridotcomdotnp.blogspot.ro/2011/10/calling-javascript-function-from.html –  Nov 06 '13 at 18:27
  • My application already use Java inside JS to RESTART activity. But this is bad design because Activity start when user don't expectedly. Then I need create activity without display it to user. This is impossible. – Tapa Save Nov 06 '13 at 18:31
  • you wrote: " But this is bad design" (completly agree with you) than redesign your app! –  Nov 06 '13 at 18:34
  • but there are potentially opportunity to create activity with immortality? – Tapa Save Nov 06 '13 at 18:42
  • it is but I would pay your app to uninstall from my device, for sure I would uninstall an app like that and I would give the minimal review. Once I want to be closed - as user - and you force me to be alive and take memory from apps which I am using, what do you expect? –  Nov 06 '13 at 18:46
  • This activity must be run in background as service. User don't see this activity. User can delete or stop my app at any time. And user don't know what is run inside my app: service or activity in background. User need result from my app and i return result to user throгпр another UI (notification, alert, file or etc.) – Tapa Save Nov 06 '13 at 19:04
  • "This activity must be run in background as service." As Service man.. not at Activity! Also check Parental controls for that to keep alive forced that Service. –  Nov 06 '13 at 19:04
  • But service don't support loading WebView with JS :) – Tapa Save Nov 06 '13 at 19:08
0

You can load WebView in background.

ZG-RG
  • 97
  • 10