Can the android lifecycle methods be called manually or if you try that you will get an exception or something like that? If it is possible to call them manually without any problems will they run in order ? i.e. If I call on stop() while the activity is in the foreground, will onPause() run first as it should?
No, it is never safe to manually call any on method - those are meant to be called only by the managing system.
Related
I want to know when user swipe to close app in recent list, but I haven't
found any methods yet. onDestroy and onTaskRemoved are not ALWAYS called, they depend on system or device.
So, which methods will be surely called when app closed, and I can use it in all devices?
OnPause(), is the method that is called everytime your app goes in background or losses it focus. So if you want your working to be done after the application losses it's focus use this method. Otherwise take a look at This answer and activity life cycle for more info.And as described in the answer above use Logs to identify which methods are ran in every device and then use them.
https://developer.android.com/guide/components/activities/activity-lifecycle.html
My app interfaces with a Bluetooth peripheral. When the peripheral wants to shut down, can I clean up the app simply by calling my Activity's own onPause() and onStop() methods? Is the fact that they call the superclass's methods likely to cause any problems?
The idea would be to call finish() after that.
Technically, yes, you can. What you should be asking is "Should I"? Which the answer is no.
Like you've mentioned, since they do call the superclass methods, there is some extra Android OS cleanup magic that happens. It may result in a successful case once in a while, but it's not guaranteed. There is a lot of things that happen in the backround that you don't want to fool with. Don't reinvent the wheel.
If there is code that is ran within the onPause and onStop methods that you would like to use elsewhere, I would create a function called cleanupBluetooth and the onPause and onStop would call and anywhere else it needs to.
If you need to actually call the onPause and onStop methods because you need to stop and halt the activity, you can do that by calling finish() (how to use finish()). The finish() method will call the appropriate Android OS magic that's needed to be called.
You can call them manually, but you shouldn't let Android do it itself when it needs to. If you wish to call those activities, you are likely not really wanting the activity any more, so you can just call finish and then android will call the relevant methods based on the activities life cycle.
You can find more info about the activity life cycle by going here
OnPause() is also a method in your activity and it will likely normal method. It overrides method of activity and you can call it from anywhere in activity and from interface also.
In Windows Phone, there is Application.LoadCompleted Event for detecting the finish of starting an application.
Note the finish means that users are able to interact with the app.
Is there similar API on Android? Any other approaches to achieve this?
after onResume() gets called it means the user can start interacting with your app. Consult this document for more information
You can override onCreate method of main Activity.
It depends what you mean by 'finished starting'. Activities, including the main activity, can be created and destroyed - and therefore onCreate() called - multiple times, for example (in the default behaviour) when the device orientation changes. onResume will be called even more often, for example every time the activity comes to the foreground. Then there's onStart(). See the Android developer docs to see when in the lifecycle each is invoked. It's true that the first time onResume() is called on the main activity will be when the app is fully started, but you won't know it's the first time without storing state somewhere outside any activity, for example on a singleton, or by subclassing the Application class, which is not something especially encouraged, as far as I can tell.
How to avoid calling onCreate method after incoming call or any other idea about this in android version 2.1?
When an activity restarts, it bypasses onCreate() so you don't have worry about that.
That's assuming of course that the Activity wasn't killed because of some lack of memory. Of course, if the process was killed, you'll have to reinitialize everything again anyway, so that's the behavior you'll want.
Check out the Activity lifecyle again:
http://www.androidjavadoc.com/1.0_r1_src/android/app/doc-files/activity_lifecycle.png
I have to run some background tasks whenever an application is activated / or starts running. Suppose u r running the application and then suddenly the device got off or any how u got out of the application without stopping it properly, then when you will again start the application, obviously it will start running from the previous state before getting out of the application. When it starts again or activated, i want my background tasks to be done.Is there any function which is called every time an application starts which i can use to initialize those background tasks? If not then how can i accomplish my purpose? Need help on this , Thanks in advance .... !!!
Whenever an application returns from being in the background, at very least it will run the the onResume() function:
#Override
public void onResume(){
//You have to call super.onResume(), otherwise an exception is thrown
super.onResume();
// and then do whatever you want here.
}
Yes, the main activity class's OnCreate method is called whenever an application is started.
From the documentation -
Called when the activity is starting. This is where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById(int) to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri, String[], String, String[], String) to retrieve cursors for data being displayed, etc.
This question might be helpful. From the accepted answer -
OnCreate is called regardless of you start the app or you resume the app.
Well - there's no such facility available in the Android SDK for you to determine whether or not your application is running - this is primarily because of the way applications are handled by Android.
For example - what would you mean by saying the application is not running? would that mean the UI s not visible, or the process related to the application is not running? Btw, I hope that you know that:
Application's lifetime != Application process' lifetime.
As for running background tasks, you can either make use of AsyncTask or the Service classes.
And if you're concerned about only the Application's visible lifetime, the you'll have to rely on making use of the onPause and onResume methods, to check on flags which you can turn on/off to capture the state of the app.