문제

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 user resumes it from background (for example user clicks home button and moves application into background and then launches it again).

I know i can do this using special permissions and granting access to system tasks, but i don't want to do this way. Also some of you will suggest to use onPause, onResume and onStop - making a boolean variable and change it state from true to false. But this only work when you have one activity - as soon as you move from activity to activity you have to add onActivityResult or something to handle boolean variable.

so maybe you can suggest something more?

UPDATED:

this is sample code from my app main activity:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    if (getIntent().getAction() != null && getIntent().getAction().equals(Intent.ACTION_MAIN) && savedInstanceState == null) {
        requestPinCode();
        }
/* .. */
}

private void requestPinCode() {
    boolean pinStart = false;
    SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    pinStart = appPrefs.getBoolean("switch_pincode", false);
    if (pinStart) {
        Intent intent = new Intent(this, PinCodeActivity.class);
        startActivity(intent);
    }
}

if pinStart is true - it means lock is activated and user must enter passcode on app start. But if you just click home button and start app again no passcode is asked. How to handle this? i want to launch passcode activity as soon as user enters app, but as he navigates inside app - no passcode. Also i want to have this in every activity, not only in main.

도움이 되었습니까?

해결책

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:

public void setLockStatus(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):

public boolean getLockStatus() {
    return getSharedPreferences("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.

@Override
public void onStart() {
    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:

public void setAppStatus(boolean status) {
   getSharedPreferences("SOMETAG", 0).edit().putBoolean("IN_APP", status)
        .commit();
}
public boolean getAppStatus() {
    return getSharedPreferences("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:

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

Now onStop checks if we need to lock:

    @Override
    public void onStop() {
       super.onStop();
       if(getAppStatus() == false) setLockStatus(true); // locking the app
       else setLockStatus(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).

다른 팁

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

Create a class that extends Application like so:

public class MyApplication extends Application {
    boolean userIsLoggedIn;

    public boolean isUserLoggedIn() {
        return userIsLoggedIn;
    }

    public void setUserIsLoggedIn(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:

@Override
public void onStart() {
    MyApplication myApp = (MyApplication)getApplication();
    boolean isLoggedIn = myApp.isUserLoggedIn();
    if (!isLoggedIn) {
        // .. open login activity
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top