1

I would like to keep the current activity state when the screen goes off and becomes locked (However, in this first case, home button should not be pressed by the user). When the screen is on, I would like to present the same activity to the user. If the user presses the home button, I would like to start another activity.

Update: When the user presses the home button, I would like to start another activity when the user relaunches activity by clicking its icon

Marcus
  • 6,697
  • 11
  • 46
  • 89
Cemil Tatoglu
  • 65
  • 1
  • 10

3 Answers3

0

An application that is not a launcher cannot capture the home button. For both screen off and pressing the home button, the app lifecycle will be the same, so it's not possible to detect that the home button was pressed.

It's not quite clear from your question, but if you want to show a different screen or "reset" the screen when the user returns to the app after pressing home, then you should probably set the noHistory flag.

This makes sure your activity is finished when the user navigates away from it, which is exactly what happens if you press the home button.

I wouldn't recommend using this flag just to show the welcome screen again when the user presses home and comes back later though. The reason is that pressing home doesn't mean a user is "done" with the app, just that something came up and switched out of your app for some reason. In that case the default Android behaviour is to return to the point where the user left when the application icon is pressed again in the launcher.

In contrast, 'exiting the app' in Android would mean that all the tasks for the app have been finished, e.g. pressing the back key for example. In that case there's nothing to return to the next time the app is launched, and the welcome activity is shown again.

I recommend you read up on the Activity lifecycle and how tasks and backstacks work in Android.

botteaap
  • 5,790
  • 3
  • 29
  • 35
  • Thank you. What I would like to do is when the user leaves the application -intentionally, that is by pressing the home button first and then relaunching the application by clicking on its icon, a "Welcome Activity", other than the current activity needs to be opened. However, if the user does not leave his current activity by pressing the Home Button, but the screen becomes off and locked, when he gets back, the activity he was using, needs to be displayed. This time, the Welcome Activity should not be started. Thank you. – Cemil Tatoglu Feb 21 '15 at 17:11
  • Well in that case I don't think you should do anything. A user pressing the home button isn't the same as intentionally exiting the app. It could be a short switch to look up something, a phone call etc. It would not be appropriate to show a welcome screen, since the user is _resuming_ the task. A real exit would be when the user taps the back key until return to the task that was started before your app was launched. In that case it would make sense to show the welcome screen again and this is how Android works by default. – botteaap Feb 21 '15 at 17:19
  • Thank you, but unfortunately in our use case the home button needs to be used as the application exit button. Correction: It was misleading to name the destination screen as the Welcome Screen. It is rather a Menu Screen to specific workflows, displaying also the update information (new notifications fo example) on the workflows. – Cemil Tatoglu Feb 22 '15 at 02:02
  • Well my answer still holds in that case. The no history flag will do what you want. And your use case is wrong :) – botteaap Feb 22 '15 at 08:52
0

What you could do is use this to detect a power button press.Then use modify onResume() accordingly to differentiate between the 2 possibilities of your application being minimized.

EDIT:I seemed to have misread your use case and understanding.But for a more advanced user and specific use case,what you are asking is possible

Community
  • 1
  • 1
Droidekas
  • 3,464
  • 2
  • 26
  • 40
0

Thank you for your answers. I detected home button press event by using onUserLeaveHint method. The function is as follows:

@Override
    protected void onUserLeaveHint() {
        //code to exit.
    }

Cheers

Cemil Tatoglu
  • 65
  • 1
  • 10