L. Gangemi
L. Gangemi

Reputation: 3300

Android How to know if the user is inactive

In my App, I have some Fragments which call another fragment in a tree-like structure.

I'd like to bring the user back to the Home page when he isn't touching anything for some time (the 30s probably).

I thought about creating a handler in the activity which handles all the fragments, every time the user interacts with something I can consider him "Active" and I participate the handler for other 30s.

In case the handler reaches the 30s, I bring back him to the Home fragment.

So, the actual question is: How can I get a listener for every element of my activity, so that I can participate the handler when I get a click?

If you need some of my code ask me to post it.

Upvotes: 0

Views: 694

Answers (2)

Gunjan Dave
Gunjan Dave

Reputation: 161

You can implement touchlistener to the container you are adding fragment. On Touch Reset your handler wait count 30 then do your actions.

  (frgamentcontainerView).setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

            if(event.getAction() == MotionEvent.ACTION_MOVE){
                //set your handler
            }
            return true;
        }
});

Upvotes: 3

Sujay
Sujay

Reputation: 3455

Callback for key, touch or trackball events in Android is onUserInteraction(). Override this method in your activity. It will catch most of the common touch events & you can use your handler in the callback.

@Override
public void onUserInteraction() {
    super.onUserInteraction();

    //Your Handler update code

}

Upvotes: 2

Related Questions