Search code examples
androidandroid-activity

Activity minimize in android


Im working on offline login based application using SQLite. For the purpose of security i need to move to login activity if the app has minimized. i tried this code in onPause function

@Override
protected void onPause() {

    Intent intent = new Intent(dashboard.this, login.class);
    startActivity(intent);
    finish();

    super.onPause();
}  

when i try to move from that activity to another also it moves to the login activity. i hope when im moving to another activity current activity is set to paused. that's why it moves to login activity.


Solution

  • you must set a Boolean to handle loginActivity:

    boolean mustGoToLoginActivity=true;
    

    when you want to go to another Activity first set this boolean to false for example:

    public void goToAnotherActivity() {
          mustGoToLoginActivity=false;
          //...
          startActivity(anotherActivity);
    }
    

    and in onResume() method set it to true. Then in onPause() method:

    @Override
    protected void onPause() {
    
        if(mustGoToLoginActivity){
           Intent intent = new Intent(dashboard.this, login.class);
           startActivity(intent);
           finish();
        }
    
        super.onPause();
    }