I want to disable the recent apps intent that is fired from the icon when you hold the home button. I am currently detecting, in the onCreate(), if the intent is fired from the quick launch dialog, and then I call finish().
This entails that when the application goes to the background finish() is called so that when the app is launched it goes through the onCreate(). This requires strategic coding when switching between activities in the application.
I guess I can fire finish() in the onResume() also after detecting where the intent came from, but a bunch of unnecessary coding can be eliminated if the quick launch intent can simply be disabled.
Can the quick launch intent be disabled?
Can the quick launch intent be
disabled?
Put android:excludeFromRecents="true" in the manifest for this activity. That will cause your activity not to appear in the list of recently used activities, which is what I am assuming you are calling the "quick launch".
Related
I want to close the application gracefully. I found two methods.
1. Using Intents:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Documentation says:
Activity Action: Start as a main entry point, does not expect to receive data.
Category Home: This is the home activity, that is the first activity that is displayed when the device boots.
2. Using Finish:
finish()
Documentation says:
Call this when your activity is done and should be closed.
What is the best method or professional method for closing the Android application? Both close the application but finish() removes the app from cache (App not in recent activities) while using intent, cache does not deleted. Should cache remove from the cell on finishing the activity?
The best way is the proper one, as it's said in the official doc :
Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().
The App is a succession of activities. Closing the app means closing the first activity of the app.
The first method you described in your Post (1. Using Intents) seems to be a workaround; it's similar to when the physical home button of your device is pressed. It lets the activity to the back stack and launch the Home (First) activity of the device.
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);
As far as I experienced, a shortcut installed on a homescreen (see INSTALL_SHORTCUT intent) can only start an activity. I have not found a way for the intent to be received by a Service or BroadcastReceiver of my app.
Thing is, the shortcut should just be a small action (here: start a timer) popping up a toast. Right now, if the app is running, it is activated - and I do not need or even want this.
I have not found a way for the intent to be received by a Service or BroadcastReceiver of my app.
That is because this is not supported.
the shortcut should just be a small action (here: start a timer) popping up a toast
You can have the shortcut point to an activity using Theme.NoDisplay. That activity can start your timer, show your Toast, and call finish(), all from onCreate() and all without calling setContentView(). Or, the Theme.NoDisplay activity can call startService() or sendBroadcast(), then call finish(), from onCreate() without calling setContentView().
In either case, you do not have a UI (other than perhaps the Toast), yet still have an activity to satisfy the shortcut requirements.
Right now, if the app is running, it is activated - and I do not need or even want this.
You may need to play around with manifest settings for the Theme.NoDisplay activity to get it to be in a different task.
I currently have two activities in my application.
The second activity in the application prompts a notification icon to be presented in the notification bar.
The intent from the first activity to the second uses the FLAG_ACTIVITY_SINGLETOP and the FLAG_ACTIVITY_CLEAR_TOP flags in the intent. The same goes for the intent used in the PendingIntent from the notification.
My aim is that the second activity is only a single activity and only created once, then when the user leaves via the home button and then goes back to the activity via the notification, the instance that was running comes back and onCreate is NOT called.
This approach works on almost all devices, however testing on one device in particular (A Sony Xperia device - 4.0.4) has shown that it doesn't work in all cases.
On the Sony Xperia the onCreate of the second Activity is always called when it is entered into by the user, even from the notification.
I have tried using singleTop in the Manifest as well without success. Is this a bug in the device or am I doing something wrong? Perhaps missing a flag I need or something along those lines?
If anyone wants more specific code or information please let me know and I will post it.
First of all, please note that it is up to Android to kill your Activity when your app is backgrounded. So there is no guarantee that your activity will stay alive while your app is backgrounded.
In any case, in my projects I end up using singleInstance launch mode for activities that only one instance should be running at the same time.
Just try to update your AndroidManifest with singleInstance launch mode for this activity:
<activity
android:name="com.your.package.YourActivity"
android:launchMode="singleInstance"/>
When would you need an Intent that takes you to the home screen? Because doesn't that mean you no longer have control of the application?
For example, what could you, as a developer, do after the following code was executed:
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
That is a much trickier question that you probably think. First of all, in real life you would not normally do that. If you are finished your activity, you call its finish() method. The android system returns you to the activity you called your activity from, which if you launched it from the home screen would be your homescreen. But if you launched it from some other application, like looking at a map of the address of one of your contacts for example, you would be returned to your contact app when you "finish()" on the map activity.
When you launch an activity with an intent, depending on the exact nature of that activity, you may open a new instance of that activity in the process your activity is running in, or you might just bring to the front a different process already running that application/activity. In the case of the home screen, I don't know exactly what happens because I don't know how the home screen is programmed as an application/activity, and how it is declared in the manifest.
For giggles, I put your lines of code in the onCreate() of the main activity of one of my applications. I got fairly erratic behavior. The intent definitely threw me out of my app and seems to have destroyed the process my application was running in in the process. (At least in eclipse, it terminated the ADB connection so I could no longer see what was happening with it.) When I went back to my main activity from the homescreen, it would sometimes go back to the main screen of my app, sometimes to the secondary screen of my app, and sometimes just pop back to the homescreen again. I imagine the other lines of code calling intents for my secondary app were part of the "state" of my app somehow, that somehow going back it would somehow come in after launching the home activity.
Obviously I am waffling around here. I'll leave this to others who might put an answer in the context of something that would really benefit from calling an intent to launch a homescreen, rather than using "finish()" to get away from the activity.