Each time I try closing my android application. I notice that when I close the close key on my android emulator close to the menu key. My app would display all the view that I have visited . It kind of weird to cos there is a default page that I expect that when the user gets to if the terminate key is pressed it should close. Well I am suppose to always call the finish() to prevent android from remember every activity that as been viewed when running the application.
Please how to I go about this.
use
startActivityForResult(intent, 0);
Instead of
startActivity(intent);
then implement "onActivityResult" and finish the app from it by calling
finish();
and do that for all your sub activities.
Related
I have an app that acts as sortofa bridge between two other apps, A and B, both of which are mostly black boxes. The user almost always stays in app A but sometimes needs to do something that my app or app B know how to handle. App A supports external actions via URL so I have a custom scheme://host defined for my app.
If the user does an action that my app handles, I get launched via the URL then do my thing then call finish() and the user's back in app A where they started (edit: same activity and etc) - perfect.
If the user does an action that app B handles, my app needs to be in the middle so it can do some translation. App A launches me, I format the message for app B and launch it. When it's done and comes back to my app, I call finish() and.. end up back in app B. Understandable I guess but I really want to end up in app A.
So the question is, how to I tell Android to put A back on top/in front without changing it? I can find the URL for app A via getReferrer() but when I put that into an Intent, it acts like I'm restarting app A instead of just going back to where it was. The doc for Intent.FLAG_ACTIVITY_NEW_TASK looked promising but it still acts like a new instance of the app.
It seems like this should totally be possible but I'm not seeing it. Java/api25.
The problem is that App B is not ending, returning control to your app. App B is launching your app, putting your app on top of it. When your app finishes, your app goes away revealing the still running App B underneath.
So basically App B has bad behaviour (at least, behaviour that is inappropriate for what you are trying to do).
To solve the problem you can either fix App B, or you can do the following:
Instead of just calling finish(), when you want to return to App A just launch it the same way that Android launches an app when the user presses on the app icon:
Intent intent = getPackageManager.getLaunchIntentForPackage("package.name.of.app.A");
startActivity(intent);
finish();
This will bring App A to the front in whatever state it was in when it was put in the background (ie: doesn't actually launch any new Activity). If App A isn't running, it will launch the root Activity of App A, as if the user pressed the app's icon on the HOME screen.
I'm assuming that both App A and App B are running in separate tasks. If that isn't the case, I'll need you to provide more information about which activities are running in which tasks.
I have an app that interconnects with Google Drive through their Java Rest API to open documents.
I am using the following code to open the document on Google Drive
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(results.get(0)));
startActivity(browserIntent);
The thing is the user can return to my app with back button if Google Drive was previously closed, but if its already opened the user can't get back to my app with the back button and instead returns to previous activities inside Google Drive.
I tried setting several flags like the following to no avail
browserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Using those flags don't fix my problem what flags can i use then?
If you are opening another application then you can't control its Activities stack which makes sense.
So if App is already running then its stack might contain activities. Which comes to screen when user press back key.
Functionality which you want can be added, like we can check user came from another Application then kill my App(third party/launched App) on press of back. But what will happen according to the user perspective if he navigated through few options and put his App in background for later work then suddenly everything clear.
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 need to make a phone call from app, and after the end of the call I need to get back to app. Thats exactly how it works in some versions of Android. But in other versions we are redirected to a native call history screen. Now I've made a service which listens to when the call ends and restarts app, but still the call history appears first. And only after pressing the "Back" button, I can return to my app. Question, is it possible to make my app restart on top of all other working apps? I.e. I need my app to be shown (not the native call history screen) right after the phone call made from the app. Thanks
I have the same function that you want and I handled it this way and it works fine.
Intent it = new Intent();
it.setComponent(new ComponentName(YourActivity.this.getPackageName(),YourActivity.class.getName()));
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_SINGLE_TOP);
YourActivity.this.getApplicationContext().startActivity(it);
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.