0

I am posting this question as I did not find the perfect solution. I want to logout user if he/she has pressed home button or is inactive on app for 5 minutes.

I have tried to use a timer as below(kotlin code),but thats not really the right way,that will just work for a single activity screen,not entire app:

 timer = object : CountDownTimer((1 * 60 * 1000).toLong(), 1000) {
            override fun onTick(millisUntilFinished: Long) {
                Toast.makeText(this@MainActivity, "Ticking", Toast.LENGTH_LONG).show()
            }
            override fun onFinish() {
                currentUser = mAuth!!.currentUser!!
                // Code for Logout
                Toast.makeText(this@MainActivity, "Finished", Toast.LENGTH_LONG).show()
                val colref = mFirestore.collection("AllUsers")
                deleteCollectiontimer(colref, EXECUTOR)
            }
        }
        override fun onResume() {
        super.onResume()
        Toast.makeText(this@MainActivity, "Timer Stopped", Toast.LENGTH_LONG).show()
        timer.cancel()
    }
    override fun onStop() {
        super.onStop()
        Toast.makeText(this@MainActivity, "Timer Started", Toast.LENGTH_LONG).show()
        timer.start()
    }
        
        

But if I put the above code in every activitys onstop() and onresume(),that is not right.So I am confused on how to logout user when inactive on entire app?

I also read something like the ProcessLifecycleowner,but did not quite understand how to use it,so can that help me in this case,if yes then how?

Is there a particular lifecycle method or a direct code which will take care for inactivity of entire application?

Any help is appreciated.

sansiad
  • 59
  • 1
  • 10
  • Possible duplicate of [How to detect user inactivity in Android](https://stackoverflow.com/questions/4208730/how-to-detect-user-inactivity-in-android) – Brandon Stillitano Apr 23 '19 at 09:33
  • https://stackoverflow.com/questions/4208730/how-to-detect-user-inactivity-in-android – Brandon Stillitano Apr 23 '19 at 09:34
  • 5 minutes is pretty long for activity that was put in background. A lot of OEM implementations will straight out kill your apps process long time before your timer finishes. – Pawel Apr 23 '19 at 09:37

2 Answers2

0

Its simple, Create a custom activity called it BaseActivity, and put your code in it, then Extends it in all the your activities.. Not forget to override the onResume & onStop method...

But above solution is best given by vikas kumar...

Arwy Shelke
  • 333
  • 1
  • 2
  • 12
0

Here is a sample which you can try at the application level.

  public class App extends Application {

    private Handler handler;

    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {
                startTrackingPauseTime();
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }

    private void startTrackingPauseTime() {
        if (handler != null) return;
        handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //some action to logout user
            }
        }, 1000 * 60 * 5);
    }
}

don't forget to register this application in the manifest if newly used.

a good read is here on how to use ProcessLifecycleOwner and other ways for this problem.

vikas kumar
  • 10,447
  • 2
  • 46
  • 52
  • so do i need to create a new class file named myapp which extends application and it is registered in manifest.Then once done will this class directly run when app is inactive?I dont need to cal this class in any activity? – sansiad Apr 23 '19 at 10:15
  • yes this is your application level component. you might need to tweak your logic accordingly. but overall the basic idea remains the same. this is what might work also check the blog i posted in the end it has more ways to explore. – vikas kumar Apr 23 '19 at 10:45
  • I am getting KotlinNullPointerException on registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks { line – sansiad Apr 23 '19 at 11:34
  • I am getting KotlinNullPointerException on registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks { line .How can I remove the error? – sansiad Apr 23 '19 at 12:10
  • yes registered in manifest as ------------ – sansiad Apr 23 '19 at 13:25