Is there any way to Detect userInterations in Android fragments?
Asked Answered
T

3

10

Could any one help me out with this situation.

I have implemented OnUserInteraction() method for Android Activity it is working fine for me.

But I want it for Fragments too.How can i able call OnUserInteraction() or is there any another way to identify userInteraction with the UI.

Trefoil answered 28/11, 2016 at 10:37 Comment(3)
It is unclear what you are trying to achieve. Please edit with a minimal reproducible exampleLegault
I would like to Close my app if it is in idle mode (Background& foreground too).OnUserInteraction is working for me for activities,but i am unable to implement it in fragments.Trefoil
The Fragment is held within the Activity. I don't understand why you think you need to implement the method there.Legault
N
2

@Sunil's answer causes java.lang.StackOverflowError so I corrected it. Below code works smoothly

Create a java class in your app named UserInterationListener and put below code there

public interface UserInteractionListener {
    void onUserInteraction();
}

Then create an instance variable in your activity, for this interface as below

private UserInteractionListener userInteractionListener;

Then implement a setter method for this variable, in your activity.

public void setUserInteractionListener(UserInteractionListener userInteractionListener) {
    this.userInteractionListener = userInteractionListener;
}

Now override the onUserInteraction method of your activity and if the listener variable is not null, invoke the interface method.

@Override
public void onUserInteraction() {
    super.onUserInteraction();
    if (userInteractionListener != null)
        userInteractionListener.onUserInteraction();
}

Now, in your fragment class, implement UserInteractionListener as below

public myFragment extends Fragment implements UserInteractionListener

also override interface's method

@Override
public void onUserInteraction(){
//TODO://do your work on user interaction
}

then in your fragment invoke your activity's userinteraction setter method like below

((YourActivity) getActivity()).setUserInteractionListener(this);

this last part is important.

Nonmaterial answered 4/1, 2020 at 19:16 Comment(0)
A
0

The easiest way to do this is to implement on touch listener in onViewCreated like so

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    view.setOnTouchListener { v, event ->
        requireActivity().onUserInteraction()
        true
    }
}

BTW this solution is in Kotlin

Apiece answered 20/2 at 3:18 Comment(0)
E
-1

There is another way around.

Create a listener in your activity as below

public interface UserInteractionListener {
    void onUserInteraction();
}

Then create an instance variable in your activity, for this interface as below

private UserInteractionListener userInteractionListener;

Then implement a setter method for this variable, in your activity. (You can even keep a List of eventlistener objects, if you want to pass same userinteraction to multiple consumers)

public void setUserInteractionListener(UserInteractionListener userInteractionListener) {
    this.userInteractionListener = userInteractionListener;
}

Now override the onUserInteraction method of your activity and if the listener variable is not null, invoke the interface method.

@Override
public void onUserInteraction() {
    super.onUserInteraction();
    if (userInteractionListener != null)
        userInteractionListener.onUserInteraction();
}

Now, in your fragment class, register for events as below

((YourActivity) getActivity()).setUserInteractionListener(new YourActivity.UserInteractionListener() {
    @Override
    public void onUserInteraction() {
        // Do whatever you want here, during user interaction
    }
});
Etching answered 2/10, 2018 at 12:47 Comment(3)
method inside onUserInteraction () doesn't get calledDyl
@ClaudeHangui Which onUserInteraction() is not called? Activity's overriden method or the method from the anonymous inner class YourActivity.UserInteractionListener?Etching
II'm trying to implement this workaround on spinner. So I followed the steps as shown above, and I register the event in my fragments, however the click on the spinner doesn't seem to work i.e it doesn't perform the expected behaviorDyl

© 2022 - 2024 — McMap. All rights reserved.