Looked at android documentation and it appears that we don't have the ability to know when an app shuts down. Whether it was explicitly by the user or automatically by the operating system.
Below is the onTerminate() documentation which is only available in the emulated scenario.
public void onTerminate()
Since: API Level 1
This method is for use in emulated process
environments. It will never be called on a production Android device,
where processes are removed by simply killing them; no user code
(including this callback) is executed when doing so.
Does anyone have any other approaches to report back when the user closes the application?
We need to know from a pilot/usability standpoint if we need to incorporate additional functionality into our future production app.
Not sure whether this is going to help you...
In my app, I'm using Activity.onDestroy() to do the cleanup that I need. I have a couple of activities - and have onDestroy() in each of them.
This is the closest I got to doing what I needed - and it actually works quite well.
Looked at android documentation and it appears that we don't have the ability to know when an app shuts down. Whether it was explicitly by the user or automatically by the operating system.
Users do not close applications on Android.
Does anyone have any other approaches to report back when the user closes the application.
Users do not close applications on Android.
Android, in this respect, behaves much like a Web browser. Users do not close Web applications in Web browsers. They might close the browser. They might close a tab. They might press the home button and navigate to a different site/app. They might choose a bookmark and navigate to a different site/app. They might drag a document into the browser and view it. They might double-click on a desktop icon and view it. And they might click some "logout" link in the currently-viewed Web site/app. Any of these cause the user to leave whatever Web site/app they are in, but none of them would be construed as "closing" the Web site/app in the same manner that clicking the close button on a desktop OS window might be construed as "closing" the desktop app.
As #Aleks G notes, there are various lifecycle methods you can override to find out what the user is doing with respect to an activity. onStop() indicates something else took over foreground input and your current activity is no longer visible. onUserLeaveHint() indicates that the user pressed HOME. And so on. But those are at the activity level, not the application level.
Make a BaseActivity extending Activity in your Application and extends this BaseActivity instead of Activity.
In this BaseActivity override the onDestroy() method.
Like
#Override
protected void onDestroy() {
super.onDestroy();
//Write the code that you want to do if the application terminates
}
Related
About Android (6.0 to the last version)
I'm developing an app and we want that the user, once he accepts all the terms, don't be able to kill the process or force stop the app. Honestly, I'm completely lost right now, because on the last versions of android, and specially some brands like Xiaomi, we are having a lot of trouble with it, and we don't know how to act right now.
In the case that it could not be possible, could at least get an alert whenever the user is killing the app?
Thanks!!
It is not possible to prevent the user from killing an app. Android is a unique system where the app has no direct control over its lifecycle but the system has. The system can (and will, when required) kill the app or any of its processes at its own will. To make your app aware of these changes, the android framework provides for various callbacks such as onPause, onStop and onDestroy which are called in succession when the user kills the app.
Side Note : There is no guarantee that onDestroy() will be completely executed when the app is killed. Do not place essential code there.
Of course, you can block or try to prevent the user from closing your app by overriding the back, home and recent buttons but it is highly recommended not to do so. Even if you do so successfully, the user has other means to close your app such as rebooting their phone.
So what to do?
You are looking for a kiosk mode app. Kiosk mode is used for single purpose phones such as at a restaurant or for a cab driver. Kiosk mode apps lock down the user to only a specific app (or a specific set of apps).
For normal apps, it is not possible to prevent the user from force closing your app. You can only get alerts by checking for lifecycle changes as described above. Moreover, it is not at all recommended to change the natural behavior of the hardware buttons on android. The user can still find a way to close your app. If your app is doing something really essential which should proceed in the background, consider using a service for that instead. Also, the user can uninstall your app at anytime if they find your app being too intrusive and you won't be able to do anything in that scenario.
Tl;dr: Use kiosk mode to prevent the user from exiting the app. This will only allow the user to access your app(s) in their device.
Usually you cannot! Even if you try to disable some buttons, user can always stop app or restart device. In addition at times, the OS will stop the App. Your responsibility as a programmer is to program around this, and give the user the feel that it never stopped. If you are doing background monitoring, you will need to use service. Users will still be able to stop service. Having said that, you can set your app as a Device Administration app, see here, which may disallow stopping, but unless you are distributing internally to a company, noone will install.
I have developed one small Android app for testing purpose. Now I need to know that how many users are online/active on my app. means any user minimize my app and go to android home, then I want notification/event and when he/she come again on my app, then also it should notify/event. Is it possible?
Unfortunately, there is no event that will tell you what you need.
But there are some workarounds.
Maybe try with this one: http://vardhan-justlikethat.blogspot.in/2013/05/android-solution-to-detect-when-android.html
or this one http://www.vardhan-justlikethat.blogspot.com/2014/02/android-solution-to-detect-when-android.html
take a look at the livecircle http://developer.android.com/training/basics/activity-lifecycle/index.html
When the system calls onPause() for your activity, it technically means your activity is still partially visible, but most often is an indication that the user is leaving the activity and it will soon enter the Stopped state
When the user resumes your activity from the Paused state, the system calls the onResume() method.
I am working on an application in which user should be able to use only one application,
he should not be able to switch application using HOME button,
the app. should be closed only when user presses the close button..
I have managed to override return button, don't know how to disable HOME button.
This is not possible to disable the Home Button in android When application is in the background because it might some application always disable the home button if it's possible. So this is the reason developer can't disable the home button when app in background. But In the Activity, you can intercept the home button.
In Activity you can disable the Home key in android. It work till Android 3.x only.
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
TGMCians is right.
Android will not let an activity and its services run completely alone on your device.
Using Override you can capture button clicks (home, back and menu) when your activity is in the foreground. If an activity/service has full control from the background you would not be able to switch for an incoming sms, e-mails, phone calls, etc either.
Personally I think its dangerous to override the home and back button together, if the activity hangs at some point, you can not get out of the app unless you restart the phone.
Personally, I do override the back key for exit, or return to main screen events, but leave the home button alone.
This is not possible using code at all on iOS, ever.
One usage of HOME button is for emergency situation.
Any house, apartment ... they all have an emergency method, like emergency door (exit), glass breaker (axe, bat ...); it saves life.
Similarly, on Android/iOS phone, HOME button saves users' lives (well, kind of). If HOME button is dead, the phone is considered dead as well as required an instant reboot for refresh. The button was designed for such a purpose, so that developers can't messed-up with everything.
Personally, I'm kinda of being thankful for Android/iOS framework team and whoever thought of this situation on HOME button. It saves my development so many times. I suggest you should think and consider the worst case possible if HOME button is not working in your app; for example, app fails to function as normal, HOME is disabled, so how to back to HOME screen, how to switch to other apps?...
The code provided by TGMCians is not working on 4.0+.
In case, you are working on Android framework, such as building ROMs, building frameworks for manufacturers, ... grep the source code with KEY_HOME and trace inner-depth to find how it works and disable it.
actually, it is possible to block the home button using the next methods:
use of security holes, at least on old android versions. this is done on some locker apps. i think some still work even on newer android versions, but it's a risk and it might be buggy on some devices. i know that "widgetLocker" and "Picture Password Lockscreen" try out those holes. i'm not sure how well they work now with them. best solution of becoming a lockscreen is #2 .
make your app a launcher app, which will handle the home button (user must confirm it of course). an example of such an app is "MagicLocker" , and in fact any launcher app...
using a rooted device. i have no idea how to do it, but i think it's very possible.
not quite a blocking method, but you could have your app full screen and on top (using the TYPE_SYSTEM_ALERT window layout type) , so home button won't be captured, but the user won't see what's going on behind your app. the downside is that any other button won't be captured by your app, since it's not really on the foreground.
Since we´re going to use phones for public use I want an app to be launched when the phone booted.
Than by filling in a code the correct activity should be started without the user being able to get into the ´phone´ software (OS).
Is it possible to overwrite all the phonebuttons, so the user won´t go to the homescreen eg, if yes, which methods are called?
Thanks
You can override the back button default behaviour by adding these lines in your activities:
#Override
public void onBackPressed() {
// do nothing
}
(The default behaviour is to call finish() on the current activity: if you do the above, you remove this finish() call).
Overriding the home button is a little bit more complicated: look at these questions:
Can I override the 'Home' button in my application?
Android Overriding home key
The Android API documentation can also show you many things.
http://developer.android.com/reference/android/app/Activity.html
You will want to make sure you cover for every part of the activity life-cycle as well as override any methods that will cause behavior you don't want (ie. button presses).
It would also be smart to look through the intents thrown by the Android OS.
http://developer.android.com/reference/android/content/Intent.html
This way you can catch any unexpected events. Knowing the platform you are working with can also help. Some Manufacturers have phones that provide specific API's (can be download from manufacturers websites) as well as hardware buttons. You should also account for this if you are trying to make a locked system.
I'm having a bit of an issue with interaction beween my app and other apps on my phone, but I'm starting to think that maybe it's working as designed? Anyway, here's the problem.
My App, call it App A is a photo-manipulation app. So a user goes in, plays around to make changes and then uses the Share button to pass it on (SEND Intent). The photo is sent to another app, chosen from the Share menu, call it App B. This stand-alone app has its own menus, completely different look and feel, etc. The user does his thing in this app for a bit, then hits the home button and goes his way to do something else.
Sometime later, he decides he wants to run my app again. He goes into the launcher, hits the icon for App A (my app), and up pops App B. Very confusing. If he happens to remember that last time he ran App A, he used the share button to get into App B, maybe he'll think to use the back button, to get back into App A. If he doesn't remember, all he knows is that he is trying to use App A, but Android is giving him App B.
(I have one app on my phone that takes over the back button for its own use so you more-or-less get stuck in App B with no way out. Ugh. You hit the icon for App A and always end up in App B)
Is there any solution to this, or is it working as designed? None of my onCreate, OnResume, onStart, etc. methods get called when this is second-open is occurring, so I can't trap it. And realistically, I can see the desire for this behavior when timelines are short - i.e. hit the home button, quickly use some other tool, and then go back to what you were doing. But with a timeline any longer than a minute or two, it gets very confusing.
Anybody else dealing with this problem? Is there a basic Android architectural issue here? Is the SEND intent being mis-used by being accepted by stand-alone apps instead of small utilities?
I think you may use Intent flag 'FLAG_ACTIVITY_NO_HISTORY'. It means starting intent never goes into activity stack.