Detecting an app coming from the background and requesting a PIN on Android

I have an application in which I need to invite the user to enter a PIN code each time the application comes from the background.

I looked at some answers from stackoverflow from @hackbod and @commonsware I applied a timeout from @commonsware and it worked fine, but my users are not happy with that. They want to request a PIN every time the application comes from the background.

Now I implement the solution from @hackbod, each time increasing the counter in onStart and decreasing the count in onStop. Android detects application appears in background

But the problem is that users change the Orientation of the current activity, the counter goes back to 0, and my application thinks that the user is coming from the background. my onStart () increments the counter, and onStop decreases the counter. I want the android to take care of changing the orientation, since I have different layouts for different orientations.

Now I do not want to use the GET_TASKS approach. All suggestions are welcome.

+4
source share
2 answers

Take a look at the life cycle of an activity . If I were going to do something like this, I would have every activity request be a pin, unless they were launched with the intention and an additional value with the current output.

Launch:

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

Start of activity:

 @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 } 

In accordance with this methodology, when an application is first launched, it requests output. After entering the contact, when the Main Activity launches other actions, it transfers the saved PIN code and will no longer request a conclusion.

You may also need to clear additional functions from the current activity when it starts another action.

+2
source

At some point, I was looking for very similar behavior. Here is the question with the generosity that I asked:

How to always start from Android?

He had many different approaches. However, each of them had some disadvantages.

0
source

Source: https://habr.com/ru/post/1440366/


All Articles