Search code examples
androidexitlogoutsession-timeoutonresume

How to get a method code that knows when user exit Apps


Issue at hand:

I would like to know if there are any method codes that is specifically used to detect or log when user pushes the app to the background by exiting the app or calling a different app. I have written this code, thinking that it can be used when the user exits the entire app.

However, the sectInt value is logged after each activity within the app has been closed. And when I enter the PropertyActivity after a stipulated time greater than inactivity_Timeout, it will log me out. This is not what I want, I would like the sectInt to actually function and logout when I exit the app instead of each individual activity within the app. Any coding suggestions?

The code below:

@Override
public void onStop(){
    super.onStop();
    //get the current time on exit
    curDate = new Date();
    Log.i("RootActivity:onStop()","******curDate=******"+curDate);
}

@Override
protected void onResume() {
    super.onResume();
    setloginButton();
    EnquiryActivity.PROPERTY = 0;
    //EDITED FOR SESSION LOGOUT
    //Get the Resume Time
    resumeDate = new Date();
    Log.i("RootActivity:onResume()","******resumeDate=******"+resumeDate);
    long diff = resumeDate.getTime() - curDate.getTime();
    long secInt = diff / 1000 % 60; //conversion of milliseconds into human readable form
    Log.i("RootActivity:onResume()","******sectInt=******"+secInt);
    if (secInt > Inactivity_Timeout){// SET EXIT SCREEN INTERVAL LOGOUT
        IdleLogout();
    }   
}

and this is the idle logout method.

 public void IdleLogout(){
    Log.i("RootActivity:IdleLogout()","******APP LOGGEDOUT******");
    SharedPreferences pref = getSharedPreferences(getString(R.string.pref_current_user), MODE_PRIVATE);             
    SharedPreferences.Editor editor = pref.edit();
    editor.clear();     // CLEAR ALL FILEDS
    editor.commit();    // COMMIT CHANGES 
    setloginButton();   // Change logout button to login
    RootActivity.alertDialog(RootActivity.this,getCustomIntent(PropertyActivity.class)).create().show();
    ////Return page to PROPERTYACTIVITY with alertdialog.   
}

Solution

  • I Have edited and changed the codings such that it actually performs an auto logout based on inactivity rather than an app logout when it is pushed to the background.

    @Override
    public void onStop(){
         super.onStop();
         //Timer needs to be stopped when user manually pressed BACK button
         stopDisconnectTimer();
    }
    
    @Override
    protected  void onResume() {
        super.onResume();
        setloginButton();
        EnquiryActivity.PROPERTY = 0;
        curDate = new Date();
        Log.i("RootActivity:onResume()","******curDate=******"+curDate);
        resetDisconnectTimer(); 
    }
    
    //METHOD USED FOR INACTIVITY LOGOUT
    public static class MyBaseActivity extends Activity {
    
        public static Handler disconnectHandler = new Handler(){
            public void handleMessage(Message msg){ 
            }
        };
    }
    
    private Runnable disconnectCallback= new Runnable(){
        @Override
        public void run(){
            //Get the Resume Time & get difference in Time for Logout
            resumeDate = new Date();
            Log.i("RootActivity:onRun()","******resumeDate=******"+resumeDate);
            long diff = resumeDate.getTime() - curDate.getTime();
            long secInt = diff / 1000 % 60; //conversion of milliseconds into human readable form
            Log.i("RootActivity:onRun()","******sectInt=******"+secInt);
            if (secInt > Inactivity_Timeout){// SET EXIT SCREEN INTERVAL LOGOUT
            IdleLogout();
            }   
        }
    };
    
    //METHOD TO CALL ON RESETDISCONNECT WHEN USER ACTIVITY RESUMES
    public void resetDisconnectTimer(){
        MyBaseActivity.disconnectHandler.removeCallbacks(disconnectCallback);
        MyBaseActivity.disconnectHandler.postDelayed(disconnectCallback, Inactivity_Timeout);
    }
    
    //METHOD TO CALL ON STOPDISCONNECT WHEN USER PRESS BACK BUTTON
    public void stopDisconnectTimer(){
        MyBaseActivity.disconnectHandler.removeCallbacks(disconnectCallback);
    }