0

i have been trying to detect user inactiveness in my app, and did find a code on stackoverflow which did marvelously but the issue is that, when am in another app, lets say Whatsapp once the timer is up in my app, my app just pop over the screen, I dont want it so, i dont want my app popping up each time the timer has count-down due to inactiveness.

Original code by Pradeep Gupta

Please, how will i accomplish that??

Here is my Code

public abstract class BaseActivity extends AppCompatActivity {

    Handler handler;
    Runnable r;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handler = new Handler();
        r = new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                Toast.makeText(BaseActivity.this, "user is inactive from last 5 minutes",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(BaseActivity.this,
                        VendorHub.class);
                startActivity(intent);
                finish();
            }
        };
        startHandler();
    }
    @Override
    public void onUserInteraction() {
        // TODO Auto-generated method stub
        super.onUserInteraction();
        stopHandler();//stop first and then start
        startHandler();
    }
    public void stopHandler() {
        handler.removeCallbacks(r);
    }
    public void startHandler() {
        handler.postDelayed(r, 1*60*1000); //for 5 minutes
    }


}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Zionnite
  • 291
  • 4
  • 13

1 Answers1

0

Remove startHandler(); from your onCreate() method and add in these two methods.

@Override
protected void onPause() {
    super.onPause();
    stopHandler();
}

@Override
protected void onResume() {
    super.onResume();
    startHandler();
}

What your code does is that it sets a 5 minute delay to execute a piece of code. Each time the Activity detects a touch or a key, it'll restart that delay.

So this works well if the user goes idle for 5 minutes within your own app, since it'll launch the VendorHub Activity.

However, you never stopped that handler when user's move away from your app, which causes your VendorHub Activity to still be launched after 5 minutes.

Simply stop the handler in onPause() and start the handler again in onResume().

Additionally, since onResume() happens after onCreate(), this also means there's no need to call startHandler() in onCreate() since it'll be called immediately after in onResume().

Jackey
  • 3,184
  • 1
  • 11
  • 12
  • i did try it out, the `startHandler()` method is not trigering inside the `onResume()` method same with the `stopHandler()` in `onPause()` but when i added `Toast` event to The `onResume()` and `onPause()` method, it did fire.. Pls what do i do – Zionnite Mar 21 '19 at 21:36
  • @Jakey, Placing the methods into the respectively `onResume()` and `onPause()` method did not do it, what am i to do – Zionnite Mar 21 '19 at 23:36