Search code examples
androidandroid-lifecycle

Application life cycle for Custom PIN Android


I'm using a child lock in my app. when the app resumes I could call the password activity to confirm the PIN. How I can implement the Application life cycle in android app.

Thank you.


Solution

  • Finally, I fixed my problem.

    I just used a global Activty and checked whether activity is running or not.

    class CryptActivity extends AppCompatActivity {
    
    public String TAG = "HII";
    public boolean wasPaused = false;
    
    @Override
    protected void onResume() {
        super.onResume();
    
        Log.e(TAG, "onResume: RESUME " + wasPaused);
    
        if (wasPaused) {
            showLockScreen();
            wasPaused = false;
        }
    
    }
    
    private void showLockScreen() {
    
        SessionManager session = new SessionManager(getApplicationContext());
    
        if (session.getPin() != -1)
            startActivity(new Intent(getApplicationContext(), PasswordActivity.class));
    
    }
    
    @Override
    protected void onPause() {
        super.onPause();
    
        if (isApplicationInBackground()) {
            wasPaused = true;
        }
    
        Log.e(TAG, "onPause: PAUSE" + wasPaused);
    }
    
    private boolean isApplicationInBackground() {
        final ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        assert manager != null;
        final List<ActivityManager.RunningTaskInfo> tasks = manager.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            final ComponentName topActivity = tasks.get(0).topActivity;
            return !topActivity.getPackageName().equals(getPackageName());
        }
        return false;
    }
    

    }