4

I have an app in which I need to prompt the user to enter the PIN every time the app comes from the background.

I looked several answers from stackoverflow from @hackbod and @commonsware I implemented the timeout approach from @commonsware and it worked fine, but my users are not happy with it. They want to prompt for PIN everytime when the app is coming from background.

Now I am implemeting the solution from @hackbod by having every activity increment the counter in onStart and decrement the count in onStop. Android detecting if an application entered the background

But the problem is when users changes the Orientation of the current activity the counter sets back to 0 and my app thinks the user is coming from background. my onStart() increments the counter and onStop is decrementing the counter. I want android to take care of the orientation change as I have different layouts for different orientation.

I don't want to use GET_TASKS approach for now. All suggestions are appreciated..

Community
  • 1
  • 1

2 Answers2

2

Take a look at the Activity Lifecycle. If I was going to accomplish something like this I would have each activity request a pin unless they the activity was started with an intent and an Extra value with the current pin.

Launch Activity:

Intent intent = new Intent(getBaseContext(), NewActivity.class);
intent.putExtra("PIN", storedPIN);
startActivity(intent);

Activity Starts:

@Override
public void onResume() {
    super.onResume();

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String value = extras.getString("PIN");
    }

    //Check pin to make sure it matches stored pin
    //Else prompt for pin
}

Under this methodology, when the app first starts up it'll prompt for the pin. After entering the pin when the Main activity starts up other activities it'll pass the stored PIN and won't prompt for the pin again.

You might also have to clear the extras from the current activity when it starts another activity.

Rawr
  • 2,206
  • 3
  • 25
  • 53
0

At some moment I was looking for very similar behavior. Here is the question with the bounty which I have asked:

How to always start from a startup activity on Android?

It had a lot of different approaches. However, each of them was with some downsides.

Community
  • 1
  • 1
Victor Ronin
  • 22,758
  • 18
  • 92
  • 184