I have an application that has many screens. Is it possible to detect if the screen NOT belonging to the application (not defined in my android manifest) comes into visibility?
onPause() will be called when an Activity loses focus (to any screen, be it your own or someone elses). When your user launches another activity from your app, you can set a flag when they do so and check for it in onPause(). If the flag isn't there, you can assume another app has gained focus.
If it's something like go back to login screen, try to read about android:finishOnTaskLaunch
http://developer.android.com/guide/topics/manifest/activity-element.html#finish
It solved my similiar problem ;-)
Related
I have developed one small Android app for testing purpose. Now I need to know that how many users are online/active on my app. means any user minimize my app and go to android home, then I want notification/event and when he/she come again on my app, then also it should notify/event. Is it possible?
Unfortunately, there is no event that will tell you what you need.
But there are some workarounds.
Maybe try with this one: http://vardhan-justlikethat.blogspot.in/2013/05/android-solution-to-detect-when-android.html
or this one http://www.vardhan-justlikethat.blogspot.com/2014/02/android-solution-to-detect-when-android.html
take a look at the livecircle http://developer.android.com/training/basics/activity-lifecycle/index.html
When the system calls onPause() for your activity, it technically means your activity is still partially visible, but most often is an indication that the user is leaving the activity and it will soon enter the Stopped state
When the user resumes your activity from the Paused state, the system calls the onResume() method.
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.
I am trying to build an AppLock like Android app but I encountered a difficult problem (for me): I managed somehow to detect when a given app is launched, by polling the system log, and bring to front a lock screen, on top of the launched app. My question is: how can i dissmis the launched Activity which must be protected (and, of course, the lock screen) if, for example, an invalid password is provided for the lock screen or the back button is pressed before entering a password?
I have tried using killBackgroundProcess with the correct package name, but it doesn't seem to work and I can't find a relevant answer anywhere.
And one more question: is it, somehow, possible to totally prevent an app from launching, form my app rather than covering it?
Well I found an alternate solution that works like a charm! Instead of dissmising a blocked app, in case an invalid password is provided (for example), I simply bring to front the phone's home screen which implicitly sends to background the blocked app. Hope it will help others with the same issue.
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.)
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.