I have an android app, ATV Trail Locator (http://www.youtube.com/watch?v=Wmk3cA2i2vI), that uses google maps.
Because my server is used for web serving and the Android app, I want to be able to kill the app in case it takes up too many server resources by setting a variable in my mysql database.
Because the application communicates with The server every second, it can get this variable and then shutdown the program if it is equal to 0.
I tried to call onDestroy() directly, but my program crashed. Do I need to use super.onDestroy() in order to kill the program without requiring back button push?
Thanks,
Have you tried calling finish() instead?
If this doesn't work, please post a code sample and your logcat errors so we can see what is happening.
Call finish() whenever you want to kill the current activity you're on. It'll call onDestroy() along with all the other required Activity lifecycle methods that need to be called.
Related
Suppose you have a news app like Yahoo.
When a user runs your app and move out of your app (without quitting the app).
After a while (like an hour) he comes back, and you want to refresh the page (if he's seeing the front Yahoo page, it may have new news to show)
Is this a thing that I don't need to worry about because android actually disposes the long lived activity?
Or is this a thing that I have to implement it myself?
If I have to do it myself, should I implement this with saving 'exit time' and comparing it with 'resume time' and so forth or are there android/ios support or github library?
You can get an idea about Android life cycle and activity by browsing this
https://developer.android.com/reference/android/app/Activity
If you create an application using react-native and deploy it to both Android and IOS, you need to use a compatible react-native module for that.
If you only consider an android application. You can override onStart(), onResume(), onStop(), onPause() and onDestroy() methods to do what u want in certain states.
eg: If u want to reload new content when reopening the application, u need to override that refresh criterion in an overridden onResume() method.
Assuming you want to update the content after however long, assuming that your data is coming from a remote source that you are loading within your React Native application - you can hit one of the React Native JS lifecycle events.
For example, you can use componentDidMount():
componentDidMount() is invoked immediately after a component is mounted (inserted into the tree). Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request.
This method is a good place to set up any subscriptions. If you do that, don’t forget to unsubscribe in componentWillUnmount().
If youre wanting to create a custom React Native Module, then you'll need to create an Android Service - but assuming you have an API endpoint for your data and the data will only be refreshed when the user is interacting with the app - I don't see why you'd need a custom module. It's all built in already.
You should use facebook.github.io/react-native/docs/appstate to check when the app becomes active, and then you do stuff.
My app needs to initially download data from two different web services (JSON) and import them into it's local database (Realm). I have two activities that need to display data from these web services. The first one (HomeActivity) is the initial activity that the app loads. The second one (LineupActivity) is created when navigating to it from the HomeActivity.
Currently, I've created an Application class (extending Application) in order to handle the web service downloading and importing. In it's onCreate(), it calls two methods, which are AsyncTasks that download and import each web service.
The reason I've added this download/import process into the Application class is for 2 reasons:
I want all the app data to be downloaded as soon as possible, so
when navigating to the second activity it doesn't need to initiate
another download.
Both these activities have swipe to refresh. They call each respective method in the Application class to re-download/import the
web service data.
Have I approached this incorrectly? Should I move the web service download/import logic out of the Application class? Also, does the onCreate() of the Application class get called more than once? Meaning, I know that it only gets called only once in the application's lifecycle, but does the Android OS eventually kill an app and have it call the onCreate() in the Application class when starting it again? I want the app to download fresh data upon startup, but not every time the user brings the app into focus.
Have I approached this incorrectly?
"Incorrectly" is a very relative term in this context.
Metaphorically, its like the context is never null but that doesn't determine boolean incorrect is true or false.
Should I move the web service download/import logic out of the Application class?
I would say Yes, as the download logic would not be related to the O.S. and the app being alive in its memory. Your requirement does not seem complex. Service would be necessary if the downloads are huge chunks of data, and if its not necessary, don't do it.
Also, does the onCreate() of the Application class get called more than once? Meaning, I know that it only gets called only once in the application's lifecycle...Application class when starting it again?
No, it won't be called more than once without app being killed and restarted. And what your are saying is correct, but for your requirement, there are probably more efficient and lighter ways to do it rather than combining it with an Application class.
For the rest of the logic, you could implement Asynctasks as a separate class and implement interfaces which are the callbacks of the result of your task. This would help in Swipe-to-refresh functionality.
In terms of documentation reference, Application class
There is normally no need to subclass Application. In most situation,
static singletons can provide the same functionality in a more modular
way.
Your case seems exactly that.
One thing you need to decide is When, how often do you need to refresh/download the data. For only start-up of the app or once daily, you can store date/day in SharedPreferences and check the value in onResume() of your Activity.
You can also implement inheritance with a Base Activity with the necessary download check logic in it and extend your classes. A Splash Screen would always help to initiate the downloads.
I suppose you could approach this anyway you want, though I like to implement one of the three options mentioned here: https://dl.google.com/googleio/2010/android-developing-RESTful-android-apps.pdf.
The presentation itself can be found here: https://www.youtube.com/watch?v=xHXn3Kg2IQE.
This does however not mention how you should sync on startup. But you could just set a SharedPreference in the application class and then use one of the patterns to sync in the background.
I'd recommend you instead of using Application to download your stuff to use a Service. If by any reason your app gets killed by the OS your dl's will never complete. Using a Service it will.
Also, you can use a Broadcast to your activity to signalize that the service has completed downloading and taking the necessary following steps.
first: I think it's bad to place the download at appliaction onCreate
as stated in documentation
Called when the application is starting, before any activity, service,
or receiver objects (excluding content providers) have been created.
Implementations should be as quick as possible (for example using lazy
initialization of state) since the time spent in this function
directly impacts the performance of starting the first activity,
service, or receiver in a process. If you override this method, be
sure to call super.onCreate().
pay attention to this part
Implementations should be as quick as possible (for example using lazy initialization of state) since the time spent in this function directly impacts the performance of starting the first activity
so any delay in download or import may cause the first activity to be delayed.
and the behavior is not clear, a black screen maybe?
Second suggestions for download/import:
1- use an AsyncTask in the first activity, where you display a small progress bar indicating the download/import process or even block the whole UI until completed (based on your business)
2- add a splash screen while downloading the data
regarding fresh data, you can store a timestamp, last_updated
and before starting the download/import process, check that value, if less than your accepted value (say 1 hour) don't start the download/import.
finally, regarding onCreate() call, i think it's not called everytime, only when app is re-created, like 1st run after reboot, or after being killed or forced close.
I want to do something when my application destory.But now, user may be use some other app to kill my app, at that time, i dont know what function will be called.I read the document and try a lot...but the onTerminate() can't do on android decive.
so how can i detect my application is killed? there are some functions will be called when app be killed? or some BroadcastReceiver will be send?
Depending on how the app is destroyed, the Android system might call onDestroy(). However, this is not guaranteed. For example, if the user uses the standard App Management utility to "Force Close" your app, onDestroy() will not be called.
If you want to save the state of your app, you need to do so during normal operation. If you need to store data temporarily, you can override onSaveInstanceState(). If you need to store data more permanently, you can use either SharedPreferences, a SQLite database, or a file. See the Android Developer docs for more details.
I also suggest that you read the Android Developer docs about the Activity life cycle.
I think what you're trying to do runs opposite of why someone would be killing an app.
When the user is trying to purposely close the app, that user does NOT want the app to do anything more.
Why do you need to check if the user is killing the app? If you need to save data, then you should design your app to save data when the user is actively using your app.
I want to know how to detect when an external app runs one of this methods. I'm working with some classmates in a project where we want to examinate the response time of other applications. The idea is to measure the time between the run of each method to get an aproximation of the response time when opening the app.
Is this possible to achieve?
Android apps are sandboxed and only expose content that they intend to expose. The methods you name are part of components that cannot be accessed directly from the "outside" world. In other ways, if an app wanted you to know when those methods are being called, they will expose that information (i.e. sending a Broadcast or maybe storing the information in a ContentProvider). You can try and see if you can get some information out of the logcat, but I cannot assure how accurate and consistent it will be.
This is imprecise, but I would monitor logcat activity. Depending on the device/VM/AVD logcat is super active during transitions (such as back-grounding and foregrounding) and idle when an app is awaiting user input.
EDIT:
Other than that, if you can do your analysis off the device, perhaps look into using DDMS?
I have a multi-Threaded android application. One of the things my application does is saves various data to a database on a server via webservices. I was trying to figure out why things were not saving to the server correctly, and saw in one of my log files, that the application objects onCreate() method and constructor were called in the middle of one of the requests going up to the server. These request are in the background and are sent via an intentservice.
I have my application set to catch unhandled exceptions and log them, and I did not see anthing in there. The application onCreate() and constructor was called, the application was kicked back to the main/first screen, the user then had to re-login, and it seems that the database was wiped(which is something else I am wondering about).
So, my main questions are: Why did the application object onCreate() and Constructor get called(why did the application get killed), why did the database get wiped when the above happened because if I do a force stop from inside of settings, applications, it never kills my db.
two words: low memory
I have the same problem. No solution for now. Try to take advantage of the onLowMemory() method, maybe the OS will spare your app.
My application object gets restarted randomly (not any time) when I am coming back from an external application (ex. camera or gallery) for onActivityResult().
Hope that helps someone.
If the application is not a service, but a 'normal' application that calles your intentservice, it is subject to the normal application lifecycle: this means it will get killed when in the background.
Look for the explaining image on this site: http://developer.android.com/guide/topics/fundamentals/activities.html
Take note of the red "Process is killed" part on the left, an the subsequent "onCreate()" afterwards.
I've actually seen very similar behaviour that was caused by a NumberFormater trying to parse a null String. After the call to parse(), the application simply reset itself back to the splash screen with no errors at all. Wasn't fun to track down, pretty much stepped through half the code base trying to find out what was happening - the debugger disconnected and the app restarted when stepping past the parse call.