Skip to content Skip to sidebar Skip to footer

Passcode On Resume From Background

i have a question if there is decent way to handle this situation: i would like my app to launch passcode activity as soon as application is launched for very first time or then us

Solution 1:

You can store your boolean value in shared preferences, that way it stays persistent in memory even if you switch Activities. For example: when you want to unlock your app (for example in onStop()) you can just set it to false with this method:

publicvoidsetLockStatus(boolean lock) {
   getSharedPreferences("SOMETAG", 0).edit().putBoolean("LOCK", lock)
        .commit();
}

Later when you want to check if your app is locked (maybe in onStart of next activity):

publicbooleangetLockStatus() {
    returngetSharedPreferences("SOMETAG", 0).getBoolean("LOCK", true);
}

Also note that this method will return true if no "LOCK" value has been set (as indicated by the second param to getBoolean).

So every Activity that you have when it starts checks our flag if the App is locked.

@OverridepublicvoidonStart() {
    super.onStart();
    if (getLockStatus() == true) {
        // show lockscreen
    } else {
       // we are not locked.
    }
}

Now we need one more flag to check if we are still in the App and never left:

publicvoidsetAppStatus(boolean status) {
   getSharedPreferences("SOMETAG", 0).edit().putBoolean("IN_APP", status)
        .commit();
}
publicbooleangetAppStatus() {
    returngetSharedPreferences("SOMETAG", 0).getBoolean("IN_APP", false);
}

So now every time we launch a new activity before we start it we have to set a flag that we are still in the App, so that onStop will know that we shouldn't lock the app. For example if your button onClick start a new Activity in the onClick we can do this:

@OverridepublicvoidonClick(View v) {
         setAppStatus(true); // we are not leaving the app.// startActivity(blabla);    
    }

Now onStop checks if we need to lock:

@OverridepublicvoidonStop() {
       super.onStop();
       if(getAppStatus() == false) setLockStatus(true); // locking the appelsesetLockStatus(false); 
    }

EDIT: also you need to setAppStatus(false); if you are actually leaving the application.

Hope this gives you an idea of how to solve it, you need to implement the back press logic yourself (when to lock the app and when not to).

Solution 2:

You can maintain some common state across activities by implementing your own application object.

Create a class that extends Application like so:

publicclassMyApplicationextendsApplication {
    boolean userIsLoggedIn;

    publicbooleanisUserLoggedIn() {
        return userIsLoggedIn;
    }

    publicvoidsetUserIsLoggedIn(boolean loggedIn) {
        userIsLoggedIn = loggedIn;
    }
}

Then use that as your application in the manifest file:

<application......android:name="MyApplication">

Then in your activities you get the application object like this:

@OverridepublicvoidonStart() {
    MyApplicationmyApp= (MyApplication)getApplication();
    booleanisLoggedIn= myApp.isUserLoggedIn();
    if (!isLoggedIn) {
        // .. open login activity
    }
}

Post a Comment for "Passcode On Resume From Background"