Home » Android » android – Looking for a more universal onUserInteraction() event – not just in one Activity

android – Looking for a more universal onUserInteraction() event – not just in one Activity

Posted by: admin May 14, 2020 Leave a comment

Questions:

I need to be able to know when the user is not interacting with the tablet/phone for a period of time. I am currently attempting to gain this intelligence using:

@Override
public void onUserInteraction(){ 
    lastInteraction = System.currentTimeMillis();
}

However, it only fires with direct interaction of the Activity (or fragment in said Activity), not any displayed dialogs. Is there a more universal way to achieve this without adding this to every dialog?

Documentation

How to&Answers:

I solved this problem by wrapping dialogs into custom layout:

public class InteractionInterceptorLayer extends LinearLayout {

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        //any actions here...

        return super.dispatchTouchEvent(ev);
    }

    //constructors...
}

And in layout definition:

<com.package.InteractionInterceptorLayer xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    // layout goes here

</com.package.InteractionInterceptorLayer>