9

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.

Ovi
  • 362
  • 3
  • 16
Hari Enaganti
  • 359
  • 1
  • 4
  • 12
  • It is unclear what you are trying to achieve. Please [edit] with a [mcve] – OneCricketeer Nov 28 '16 at 10:43
  • 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. – Hari Enaganti Nov 28 '16 at 11:23
  • The Fragment is held within the Activity. I don't understand why you think you need to implement the method there. – OneCricketeer Nov 28 '16 at 11:33

2 Answers2

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.

Asad35Waheed
  • 121
  • 11
-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
    }
});
sunil
  • 6,444
  • 1
  • 32
  • 44
  • method inside onUserInteraction () doesn't get called – Claude Hangui Jul 01 '19 at 14:10
  • @ClaudeHangui Which onUserInteraction() is not called? Activity's overriden method or the method from the anonymous inner class YourActivity.UserInteractionListener? – sunil Jul 01 '19 at 14:33
  • 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 behavior – Claude Hangui Jul 01 '19 at 14:36