I want to know if the user clicks on the Home button (like listen to Home button).
Because I saw that is there now way to do that, I tough about an idea.
Maybe if I will create a launcher I will be able to handle to Home button...
But since I don't want to really create a launcher, so I want to create a launcher, that opens the previous one.
How can I create such a launcher? Or is it possible to use BroadcaseReceiver to listen to Home button?
HOME button can not be monitored nor reacted to and there is no workaround.
As for the launcher:
First, you would need to fetch the ResolveInfo for the current launcher and keep its package name somewhere, like SharedPreferences.
Later, the user would have to accept your launcher as the default launcher. After that, you should make your launcher's onCreate() method's only job to create an intent which will open the previously saved package and then immediately call finish() on your launcher.
However, I am pretty sure you will stumble upon some problem along the way, as this is Android. Anyway, be my guest to try and post the result here, the concept is really interesting.
Related
I have a login activity. After login, the Main activity is started, which uses fragments for user navigation. I want a button in the nav drawer of my main activity which will completely close the app
Now, I have seen many many threads on this and have tried implementing their solutions. For example:
I have tried finishAffinity() in my Main activity, which should close the current activity as well as all parent activities
I have tried using finish() on the Login activity as soon as I bring up the Main Activity, and then calling finish() again when the user clicks the button
The highest voted answer for this question: Close application and remove from recent apps/, also does not seem work. First, android:autoRemoveFromRecents="true" requires API > 21, but even if I set the minimum SDK version to 21, the app still remains in the list
Finally, I have tried using an Intent when the user clicks the quit button and navigating back to the Login activity, and setting flags with an exit extra, and then finishing the Login activity (i.e. exit android application programmatically)
None of these are working. They will all close the Main Activity, and maybe even close the Login activity. But if the user clicks the app list/current apps/open apps key (the square soft key on most phones), the app is still visible there. When the app is clicked in that list it will take me back to the Login activity screen (I'm unsure if this is starting the app from fresh, or whether its just taking me to the previous Login screen which didn't close)
Out of desperation I have even tried System.exit(0), which I know is bad, but even that doesn't remove the app from the app list
So, how do I programmatically completely quit an app and remove all traces of it being open?
EDIT: I was too hasty in claiming one of the answers below didnt work (see italics above). The answer does remove the app correctly
I think this is the solution you're looking for.
You might consider having another activity named ExitActivity which will be called when you try to exit from your application. The trick here is, the ExitActivity will have android:autoRemoveFromRecents set to true in the manifest file, so that your instance will be cleared automatically from the recents.
Based on my research:
There is no way to force quite an application in android. You can only pause an activity. The discretion to quit an app lies totally with android framework which it does on checking the memory and resource utilization of apps. I could not find the official link for now, but I had read it earlier.
manage it by the OS. bring up the home screen.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I am working on a fitness app which has a home activity which launches a workouts activity which launches a specific workout activity. In the workout activity, one may start a workout. Thereafter, one might want to then press the Home button and launch a music player or perhaps the web browser. At some point, one would probably launch the app again to return to the already running workout, but that ends up launching a new instance of the app. When I set the launchMode on the home activity to singleTask, it simply goes back to the existing home activity when I tap the launcher icon. What I would like is for it to go back to the workout in progress, which is where you would depart the app.
Essentially, I'm looking for behavior identical to iOS where it would simply restore the app to its current state if you "relaunched" the app and it was still running.
It is supposed to work as you've described. In most cases, it actually does work like that. However, there is a long-standing nasty Android bug which causes the behaviour you've described. This happens when you launch the app for the first time from an IDE (like Eclipse) or by clicking the "open" button on the Installer screen. To see if this is what you're seeing, just do this:
Go to Settings->Applications, choose your app and click "Force close"
Launch your app, do something, press the HOME button
Launch your app again.
You should return to where you left off. If not, something else bad is going on. If that is the case, please post your manifest in your question, because the problem is likely in these.
Don't try to use special launchModes to fix this. This just creates more problems.
See this answer for more information about the nasty long-standing Android bug.
I'm working on large project, so there is some logic for saving application state, and then opening correct activity(fragment) when it comes from background.
But anyway, I've found that if user navigates through my app and then minimize it - android opens it from background in different ways in following cases:
User taps on app icon (behavior: user see home activity, doesn't matter where he was, when application was minimized)
User select app from android "recent apps" (behavior: user see exactly what he saw, when the application was minimized)
So, what is the difference between launching application from background by this two ways? I always thought, that it is the same mechanism, but, obviously, I was wrong.
Thanks for any answers
You should pay atention on the folowing docs Activity and Tasks. In short words: if user start app from recents you will receive onRestart before onStart (without onCreate it means that your app was just "suspended"). You able to save screen state using onSaveInstanceState(). But in general starting from icon and from recents - different application behaviors and you should provide proper code for this ways.
UPD
As described below root cause of unexpected behaviour was additional lunchmode attribute.
From what I experience as an Android user, both are same.
The difference we usually see is how we close the app
Press back button until app close / finish()
On this state no matter how we open the apps it will go to the main screen
Press Home button
On this state depend on the app. If the app does not handle any Activity the app will same with the first state. But if the app handle something like when onPause() the Activity then finish() the apps, then whatever you open with app icon or recent apps will provide the same result.
Correct me if I am wrong
I have looked everywhere for something related to this. I want to launch a shortcut, based on the shortcuts installed on a device.
A simple way of putting this is my user selects one of his shortcuts through my application. When the user initiates the shortcut through my app, it will perform the exact same thing as if he/she is on the home screen and selects the shortcut.
I can do this individually with the system shortcuts, but I want to have more flexibility for other shortcuts installed on the device.
So basically, I need to figure out how to bring up the shortcut picker (the same list that appears by long pressing a home screen and selecting shortcuts), get the intent to launch that shortcut and store that within my application data for use later. I don't want to actually initiate the shortcut when the user selects it, I just want to remember it for later.
Any help with this would be greatly appreciated :)
I think you are looking for this: https://developer.android.com/reference/android/content/Intent.html#ACTION_CREATE_SHORTCUT
Hi I have application with more than 20 activities.I want to close my app when Home button is pressed.
There is no notion of "close my app" in Android. Android will get rid of your activities, followed by your process, after some period of inactivity by the user.
You could use the launchMode and clearTaskOnLaunch flags on your root activity from your AndroidManifest.xml:
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
When you again start your app, all activities will be killed.
You don't want to do System.exit() -- that's not how the Android Activity Lifecycle normally works (read this also).
What you should do is move the App to the background with moveTaskToBack(). Android will then keep your app running in the background, and kill it if it's unused and something needs its resources later.
If you want it to close all of the open Activities when your App is no longer visible, you can set noHist = "True" for all of the child activities (leave the main activity with noHist = "False", though). This will make it where instead of reopening your application on the last Activity they were on, it will open it on the "main" activity (i.e. it will be as if they just restarted the app).
Anyhow, read through the following answers for more information: Close application and launch home screen on Android
I have the same problem. Im writing a banking app and am required, by contract, to log off the user (or exit) when the app is put into background. There are obvious security concerns there.
There are a couple of ways Im looking to do this:
1. Intercept home button (and back button for the root activity) key press events to call logoff and/or finish()
2. In the onStop() method, for every activity, detect whether the activity is being stopped due to a new activity being show - if not, assume app is being put to background so logoff and/or finish()
The first may not work if a notification is brought to the front then the user clicks home (I havent investigated yet). Or maybe there are other ways for an app to be put into the background without pressing these buttons
The second way sounds messy & difficult to maintain
Id welcome any other ideas
Drapes
I know android has been designed this way, but it seems naive to think that apps wouldnt want an applicationOnStop event
Hi guys what I understood is that u need to know when app goes in background and how to detect it and if I am wrong plz correct me----
The user can go in background if ur app does not provide any way by pressing Back key or Home Key.
You need to use methods "dispatchKeyEvent(KeyEvent event)" to get home key event or back key event and after getting the event you can execute your task.
you can even restrict user from pressing any key but u can not control the home key.