0

I am building an android application and one of the requirements is that the application needs to let the user define a passcode to lock the application so, once the passcode was set by the user, the passcode will be required to enter or re-enter the application.

Instead of passcode I am going to use this android library, https://code.google.com/p/android-lockpattern, that provides the same pattern behavior as the home lockscreen, using pattern.

So far, thats ok.

If the application is open on the first time, which mean, onCreated() is called, I can check if there is any passcode set and if yes, redirect the user to the screen where he/she needs to enter the pattern. If pattern valid, ok, user is in, if not, show an error and leave the application. This scenario is ok.

However, if the user hits the home button and re-enter the application, onCreated is not called, so now there wont be any passcode check. So I was looking at the other activity lifecycle's methods like onPause, onRestart, onResume, etc... all of them are called when the user re-enter the application, and ALSO, when the user navigates back to the Activity by pressing the back button.

So I am a bit confused/lost on where should this passcode check be besides the onCreated? I dont want to show the passcode screen when the user is navigating thru activities and suddenly returns back to the MainActivity.

How can I make sure the user will be required to enter its passcode either when starting the application or re-entering the application.

Thank you TL

Thiago
  • 5,152
  • 11
  • 35
  • 44
  • Here is another SO question that might help. The accepted answer requires an extra permission, and the other answer explores some alternate ways to do it if you don't want to include the permission: http://stackoverflow.com/questions/13292547/activity-losing-focus-due-to-another-activity-in-the-same-app-vs-a-separate-app – bracken Apr 02 '14 at 18:20
  • @bracken that solution is perfect, it actually makes me achieve what I need, not sure that is a good design implementation for my problem, but it solves the issue. – Thiago Apr 03 '14 at 19:35
  • @bracken can you make your comment an answer so I can accept it. Thanks – Thiago Apr 10 '14 at 16:55
  • Here is another SO question that might help. The accepted answer requires an extra permission, and the other answer explores some alternate ways to do it if you don't want to include the permission: [stackoverflow.com/questions/13292547/](https://stackoverflow.com/questions/13292547/) – bracken Apr 10 '14 at 17:00

3 Answers3

0

Check out the Android Activity Lifecycle diagram. I think you're probably looking for onStart, which fires every time a user opens the app, either from the background or as a fresh instance.

Here's another SO question relating to the same thing: Android onCreate onResume

Community
  • 1
  • 1
Eric Dand
  • 1,106
  • 13
  • 37
  • Hi Eric, I checked the lifecycle and also tested within my app: onStart() is called when the app opens as a fresh instance, when its opened from background and when navigating back to the activity. – Thiago Apr 02 '14 at 18:00
  • Yep; that's what it does! Is that not what you wanted? – Eric Dand Apr 02 '14 at 18:01
  • if I check for the passcode on onStart, then when the user press the back button and returns to my activity, then I will present him with the passcode screen... I shouldnt as the user is using the application. I wanted to present the passcode screen when its a fresh instance or when the app comes back from the background... the application, not my MainActivity... also, the application can go to background on a second activity, not the MainActivity which checks for passcode. – Thiago Apr 02 '14 at 18:04
0

I would recommend putting this on a Timer or something similar. Whenever the user interacts with your app, the timer (held in an extension to the Application class) starts counting down - from say, 3 minutes.

Once the counter expires (or, when your activities check in their onResume whether the timer has expired), it starts your top-level login activity again, clearing the back stack as described in this answer.

This has the benefit of the timer still working while the app is in the background (assuming it's on a separate thread) and the user can still accidentally turn their screen off (for example) and not be forced to log in again. My bank's app does something like this.

Community
  • 1
  • 1
Adam S
  • 16,144
  • 6
  • 54
  • 81
0

When you switch between Activities, you always call them with an Intent object. The onCreate() method is executed when the Intent is created, but if you unlock your phone, the executed method would be onResume().

You have to control the passcode check in the onResume() method. You can't show it every time this method executes, because pressing the back key also executes it. Having in mind that you don't want to execute the passcode check only when you're pressing the back key for returning to the previous Activity, maybe you should check, on the onResume() method, if the Intent comes from the Activity or Activities that the user is supposed is coming from. So, if is not any of these Activities, it will show the passcode check.

PD: There's also a way to detect if the user comes from unlocking the phone. You need to detect if the Intent is an ACTION_USER_PRESENT Intent. There's more info on this post: https://stackoverflow.com/a/4602341/2367998

I hope it'd bring you help!

Community
  • 1
  • 1
devrique
  • 526
  • 4
  • 10