No Background Application in Android - 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

Related

All activities are closing on home button press

Thank you for taking the time to help.
I have created an Android application thats starts up with a splash screen, then depending if the user is logged in, the application opens either the login or main activity.
When the user presses the home button and then click's the application icon I would like the application to resume at the last activity the user was at (For example the login screen).
This works fine when the APK is installed onto the device via Android Studio, but if I try to install the APK manually (the exact same APK), every time I press the home button and reopen the application it acts as if I killed and then started the application (The splash screen starts again).
Any idea why this could be happening?
Thanks
add following line to your manifest in your launching activity means 1st activity
android:launchMode="singleTask" android:clearTaskOnLaunch="true"
on other activities add:
android:finishOnTaskLaunch="true"
What you want to achieve is already the default behaviour of Android, but there is one possible way this can happen which is if your device need more space in memory then it may kill other applications which are on pause state. I believe that you're running into that situation and i'm afraid you don't have anything to avoid that.
Refer to
Edit: By the way, you can check that by logging something onDestroy and onPause method to see when your activity pauses and when it gets destroyed by system. Until it gets destroyed, you should be able to see the behaviour what you seek.
I met the same problem.
Сhange android:launchMode="singleTask" to android:launchMode="singleTop" helped me.

Android- Application Settings Force Stop

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.

Android: How do I restart my app by activity that I stopped by pressing the home?

When i stop my application pressing home button, i need that the app restart from the same activity when i open it again.
Now my application always start from main activity :(
(i don't know why but on emulator work correctly...)
There are a lot of options for preserving the state of an application - the ost common of these include using the bundle passed in OnCreate to initiate the application correctly.
I suggest you look at the android developers documentation as regards to activity lifespan.
When you begin an Activity, store a SharedPreference that records which Activity you are on. Check this preference when you enter your main Activity and, if set, jump to that Activity.
(If you are just looking for a way to get back to your Activity and don't care about the code, use the Recents menu to get back to your Activity. On devices with dedicated keys, long-press Home.)

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