Android- Application Settings Force Stop - android

I didn't use any services in my application and closing the application by using
this.finish();
but my application still not stopped properly and it is running in background.when i go to application settings the force stop button is still enabled.
kindly share your views on proper exit of android application.

It's quite possible you have another activity around.
From the Android docs, see Activity.finish():
Call this when your activity is done and should be closed. The
ActivityResult is propagated back to whoever launched you via
onActivityResult().
There's no promise made that the activity will be closed right away on calling finish(), only that this is something that should be done. Usually this does happen right away, but without seeing your project I cannot comment further.
Note that Android, unlike iOS, doesn't really have a well-defined notion of an app. "Apps" can share activities and so on. For example it's not hard, but it's also non-trivial, for an "app" to know that it will go to background or that it has resumed.

Related

UI not working properly after system killed app

My app uses a pagerTabStrip with fragments. When the app goes to background and other memory intensive tasks run in foreground, the app will be killed. Then, if I recall the app, the fragments buttons onclick methods dont fire anymore. (side note: the color of the buttons is then wrong too, but the "visual click" down->up is working)
It is working fine when I manually kill the app or when it's just short term via onPause/onResume in background. It seems that in this system-kill-case sort of image of the app is taken and reloaded (incorrect).
If there is no direct way to fix this, I would like to catch this event, to just recreate the whole fragment, but I can't. I tried onTrimMemory and investigated the call order of onStop/onDestroy/onRestart ... but I couldnt figure out, when this "special kill" happens.
Thank you!
Got it.
First, to reproduce the issue, just launch the app, send to background and then hit the red circle-cross at the left pane of the logcat output frame in android studio as described here:
https://medium.com/inloopx/android-process-kill-and-the-big-implications-for-your-app-1ecbed4921cb
My workaround is to save the processID in onSaveInstanceState and to check if this changed in onCreate as described here:
https://stackoverflow.com/a/47483856/6053524
If so, the app has been killed by system and my issues of not proper working UI occured. To fix this I load the initial Activity via new Intent/startActivity to get a clean new application.

Android: how to know previous app

I have 2 different applications A and B and I want to create a special animation from B to A, that is when A is opened after B was visible. This means that I need to somehow know the previous app after which my app was opened. I can have different scenarios of going from B to A - using Recent Apps (multitasking) button, using Back button, using Home button (application A is a custom home screen). Are there any ideas how to do this? Some functions in ActivityManager might help, but they have comments in documentation saying not to use them for implementing logic and control flow.
Not sure if this will work across different applications, but how about getCallingActivity() or getCallingPackage()?
If that doesn't work, could you pass along some 'extra' data in the bundle when you launch the intent that indicates the launching application?
I managed to figure out how to implement this, its working for me.
I used this answer, but replaced the ActivityManager.getRunningTasks() with ActivityManager.getRecentTasks() supplying RECENT_WITH_EXCLUDED | RECENT_IGNORE_UNAVAILABLE, and took the component name from baseIntent member of the result. The info at index 1 is the one that was running before the current app was opened, irrelevant of the fact how you get back to your app - back button, home button, recents button or opened from another app.
NOTE: This works when your app is started, like in onResume() but doesn't work when your app is closed (when called in onPause()) because the new task is not yet loaded into the activity manager. So if you need to also know to which app are you going then it might be a bit more complex.
NOTE2: Although the documentation tells not to use the API above for any kind of logic and control flow, I saw that the multitasking/recent app's code is doing exactly the same, so in my opinion it should not be as risky as they write it docs.
NOTE3: Don't forget to follow all the steps in the answer I mentioned above, like adding needed permissions, otherwise you will get exceptions. Being part of the system in my case makes it much easier for me.

No Background Application in Android

I am making an application.
Now what i want is that whenever user Press HOME key on android, it does not go in Background Mode.
Can any body give any suggestions how to implement all that?
Please Reply.
Thanks a bunch in advance
You cannot (easily) force your app's process to exit, nor should you. You can force an activity to disappear from the stack when the user navigates away from it by adding android:noHistory="true" to the manifest for that activity. But that will apply even to another activity in your app. You can also add android:finishOnTaskLaunch="true" to force an activity to be closed down if the user launches it again from the home screen. However, the activity will run until then (or until the system shuts down the process for other reasons).
Android naturally keeps your application alive, but calls the onPause because it's not an active activity, then calls onResume when the user goes back to your application.
If you need to run code without a UI or you want it to be checking something then opening your app at some point You can use a service.
API Demos have some great examples of how to use a service

How to close an application when the Home button is pressed

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.

How to exit an application completely in ANDROID

I have an application.In that if we press on home button app closes but when i launch the app it resumes where i stopped.I mean it do not closed completely.how to solve this problem.
When you press Home button, onDestroy method of the current activity is called. You can perform any shutdown operations there. Design of Android doesn't have a concept of explicit application shutdown, so that the user can continue on the same activity where he started.
You are trying to copy desktop application behavior (the application is shutdown explicitly) to Android with different usage patterns. While this is understandable, in most cases this will contradict to other applications behavior and will annoy users. So if you have anything to shutdown, do this in onDestroy method.
You can make your android app return to the root activity each time you open it by modifying your AndroidManifest.xml to include
android:clearTaskOnLaunch="true"
in the desired <activity> declaration.
Android does not allow you to terminate the application at any time. Application lifecycle is maintained by the Android OS itself. You are not supposed to meddle with it. Unlike desktop applications, Android application lifetime is determined by the OS itself. You can only end an activity.
For more Info refer to this bug http://code.google.com/p/android/issues/detail?id=1572
You need to call finish() in your onDestroy() method.

Categories

Resources