Android : How to determing home key is pressed - android

How do I determine if the Home key is pressed? If it's pressed I want to do some handling and finish the activity since I don't want it to resume next time it's started. I can't handle it in onStop, since from the activity I start another activity(so onStop will get called even in this case).

How do I determine if the Home key is pressed?
You don't.
If it's pressed I want to do some handling and finish the activity since I don't want it to resume next time it's started. I can't handle it in onStop, since from the activity I start another activity(so onStop will get called even in this case).
There are many ways in which a user can leave one of your activities, including:
By you starting another activity
By the user pressing HOME
By the user pressing BACK
By the user receiving a phone call, or some other activity popping up out of nowhere (e.g., alarm clock)
By the user responding to a notification
By the user rotating the device, putting it in a dock, or otherwise triggering a configuration change
Generally speaking, you have no idea which of those has occurred. isFinishing() will cover #3, and there are a couple of ways to identify #6, but the others are indistinguishable from a system standpoint.
If you wish to treat scenario #1 as special -- doing something different because the user is moving to one of your activities rather than something else -- then that's up to you to implement.

Related

How to properly finish (reset) an activity as soon as the user leaves it

I have an Activity that I consider a critical operation (Specific communication with another computer over Bluetooth) and I want to make it so that when the user leaves the activity, it cannot be resumed to that state. With other words, if the user resumes the activity it should be recreated.
Since this activity uses Bluetooth it might start one or two activities for result (Enable-Bluetooth activity and Request-Permissions activity) and therefore, I cannot simply finish() the activity in onPause().
By leaving the Activity, I mean presses the home button, takes a phone call or presses the multitask button
I have experimented with some Activity Launch modes (like singleTask) without success.
I already call super.onCreate(null) in the Activity's onCreate() method, preventing it from recreating to a specific state after it has been destroyed, but I want to reset the activity whether onDestroy() has been called or not.
Does anyone have any suggestions on how this should be done correctly?
Edit:
The question in the Possible duplicate explains how to quit an application and it's subtasks completely (whereas just finish() would suit my needs perfectly - if I knew where to call it). This question is about finding a clean way to not resume the previous state of the Activity.
If you never want a state persist once you've left via the home button, or perhaps even when the screen turns off, the simplest thing is to work with the lifecycle events available. It's a whole lot simpler than trying to work around Androids design by doing things like forcing the close of your app.
Since everything needs to be setup each time someone returns to the app, you can move all of your setup logic out of onCreate and into onResume. Then, perform all the required cleanup (kill your BT connection, etc) in onPause. The only possible gotchas are related to things like changing screen rotation/ opening the keyboard which might trigger lifecycle events that you didn't intend. That might make your program less responsive if you have a lot of long running tasks on the UI thread in onResume.

Can I call a method right before my app closes?

Here's the situation:
I want to check if the device is rooted or not every time the app comes to foreground (either because they're launching the app through App Drawer or coming back to the app via Recent Apps list). This check is done during onResume(), and it's working well.
The problem is that the intention is to check for root only once when the app comes to foreground, not when user is currently using the app. Since onResume() is called when an Activity comes to foreground, this means the check is done multiple times even when it's not needed, which comes at performance cost.
I thought of using a static variable to lock it, locking just before I perform the check to ensure the check is only called once. This is fine and dandy, but the problem is when to do the unlock?
onPause() is called before another Activity comes to focus, which
would negate the lock. I tried to use isFinishing(), but if a user
presses the back button, the activity is destroyed, which resets the
root checking lock and renders it less desirable. EDIT: Also, the Activity is not finished if the user presses Home button, which means it's also not reliable enough
onStop() and onDestroy() are not guaranteed to be called, and
they're also called if the user presses back button.
Is there a way to call a function exactly once when the app moves to background, without restricted to the constraints of onPause() above? I searched through the Activity, Application, and BroadcastReceiver documentation but couldn't find any mention about such a thing
In the end, I decided to use a combination of onUserLeaveHint(), custom startActivity() and onBackPressed() to do it, with some private static and non-static variables
I used onUserLeaveHint() to detect when user is going back to Home
or to Recent apps list.
onBackPressed() is used to tell the app that it is used for
navigation, while another onBackPressed() override is written at the
app entry point to detect when user is using back button to go back
to Home (can't believe onUserLeaveHint() doesn't account for that)
the custom startActivity() makes sure that onUserLeaveHint() is not
called when another activity is started. While onUserLeaveHint() has issues with forced interruptions (such as user receiving a call), it is deemed acceptable for now.
I did some research and found this blog but it makes use of onStop(), which is not guaranteed to run
Thanks for all the responses. I understand the concern about my security approach, but the question is about detecting when the user is leaving the app

Do i get a Android Broadcast or Event when the app comes back to focus

I want to be notified when my Android App comes back to focus or is started. The tricky part is, i don't care for the events which appear when switching Activities within the App. The Events i am interested in are:
App is started
App is reactivated on any Activity (brought back to front by the user)
I tried handling it with the onStart() and onResume() Methods, but they fire every time a Activity is loaded and call onStop even when they just switch Activities within the app
I checked the lifecycle (http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle) but even the onDestroy() is called when solely switching Activities within the App.
How can i determine if my App is reactivated or just switching Activities?
you have to override Application class for **App is started** this will call once every time when app is open.
Two possible solutions come to mind, neither of them is very clean.
1) Use a timer
Every time in your onPause() method, you schedule a timer that sets a flag after 1 Second (or something longer, depends on your exact use-case). In each onResume() you check if the flag has been set, e.g. your App has been inactive for longer than the timer interval. If the flag is set you know the user did something else between the onPause() and onResume()
2) Set a flag manually
Every time you trigger a switch between Activities in your App, you set a flag somewhere in your application. In your onResume() method, you check if the flag is set, e.g. the switch was triggered by your App. If it is not set, the user comes from outside of your App.
Both methods, however, can't discriminate any further between possible use-cases. All these cases will look the same to you:
the user presses the home button and resumes the app at any given time later
the user turns the screen off, and back on again
the user gets a phone call and then immediatly returns to your app
This might lead to undesired behaviour of your App. I don't know what exactly you want to use this mechanism for, but you have to think very hard about all the possible cases, and if you want them to trigger your behaviour, or not.

Hitting Back Button and then Resuming Activity

I am developing a game for Android and have what I imagine to be a very common scenario, but I can't figure out how to do it.
I have a Title Screen where the user can select "New Game". When they start up a game, I push a GameActivity onto the stack. Then if the user hits the physical back button the phone, the application goes back to the TitleActivity, as I intend it. However, I need a way to allow the user to resume the game at this point, but I don't know how to get back to htat instance of the GameActivity. Any help is appreciated.
You can't guarantee that you will get back to that instance of GameActivity you need to save the game state somehow, and make your GameActivity able to resume from that saved state.
Make sure you understand the Activity lifecycle. The instance of your activity can be killed at any time when it isn't visible, so you need to save state at the proper point (onSaveInstanceState).
The answer I would give is: don't do this.
Finishing an activity (which is what happens by default when you press back) does finish that activity. You don't return to it.
If you want to allow the user to press back to pause your game into a menu, capture the back key with onBackPressed(), and instead of allowing the default behavior of finishing the activity, simply show your menu in-place in that activity. Generally I think for games making the core game UI be one activity that is the base activity launched for the app makes more sense than trying to split it into activities. Games are just special that way.

restoring the task to it's last state in android

I'm not talking about restoring application state (using Bundle or configuration change or even SharedPreferences) i'm talking about the fact that i have a certain task, that is a base activity: HomeScreenActivity and from there i can have a series of activities opened for the user's flow.
I want in case of application's death or user killed his own session to restore the whole task, not just one activity, i mean to restore the whole activity stack i had before the application was killed. if the user was in my billing screen and left off my app i want to take him back to that screen, but i want that if he'll press back he'll go back to my search screen or filtering screen, whatever was there before he left off.
I have no problem saving the activity's order in sharedPreferences and know the order once i start my home screen, but i wonder if there's a way to tel landroid to start an activity, which sould only enter the task's stack and not call the activity's onCreate method unless i actually get to it(with back key).
Perhaps someone will have a better idea, but the only thing that comes immediately to mind is to add a "resetup" flag so that you could sort of replay a script of the user's previous activity navigation actions within your app, having your activities start each other up in turn but due to the resetup flag bypassing most of their code so they don't really do anything except essential setup and start the next one.
One complication is that it's probably the activity they were actually in which android will resume.

Categories

Resources