The status of whether to ask for the password or not (if the last login is still valid) you would save it in SharedPreferences
. Here you have to determine what you are going to save to tell you whether or not to ask for the key, it can be from a boolean for a key for only one time, a date and time of expiration, a token, or a combination of these things.
Starting from this, being in SharedPrefrences
, it is available for any Activity, and also for any Fragment.
But implementing the control as a Activity
separated, a better separation of responsibilities is achieved, given that the control would do the Activity that asks for the key and only if it is verified the correct entry of the same, would call the Activity
Real% that you want to execute.
In other words, if the Activity
that does the control, does not validate the key, the Activity
that you want to protect, does not get to run.
So, in the GridView
what you're actually colcoating is the Activity
that does the key entry control.
If it is verified that you need to enter the password, show the form. If the key is correctly validated, you update SharedPreferences
, and you throw the Activity
protected with a Intent
.
And if it is not validated, you return to Activity
of GridView
.
Most likely the Activity
that the control throws the Intent
to open the Activity
protected from onCreate()
.
This gives an error if you want to do in the same Thread
that is running the onCreate()
, that's why it is usually done using a Handler
.
final Intent intent = new Intent(this, ActivityParaIniciar.class);
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run() {
ActivityActual.this.startActivity(intent);
finish();
}
}, 100);
ActivityParaInitiate : is the activity protected
ActivityActual : it is the activity that performs the control
Note: It is important to finish () for When you return, do not go back to this activity.