How to detect if it is Recents button, Multi Window mode or home button onUserLeaveHint()
Asked Answered
B

1

6

In my app, I am relying on onUserLeaveHint() method when the user presses the home button, but this method is also being called when you are starting multi window mode in android 7.0 by long pressing "recents button" (which I don't want to perform same thing that I do when home button been pressed). So I want to know if there is a way to detect which is which. Cheers!

Note: onMultiWindowModeChanged() being called after onUserLeaveHint()

Boggart answered 4/10, 2016 at 14:18 Comment(4)
If it isn't deprecated in 7.0, it will still workPylon
It still works but it doesn't help me achieve my purpose. I don't want it to be called when user activate multi window mode.Boggart
What does calling isInMultiWindowMode() from onUserLeaveHint() return even if onMultiWindowModeChanged() has not been called yetHinckley
It returns current mode as boolean that we have no idea at that time if we are changing to multi window mode or to single window mode. @ScrotosBoggart
N
0

I think this is what you're looking for.

HomeWatcher mHomeWatcher = new HomeWatcher(this);
mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
    @Override
    public void onHomePressed() {
        // do something here...
    }
    @Override
    public void onHomeLongPressed() {
    }
});
mHomeWatcher.startWatch();
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

public class HomeWatcher {

    static final String TAG = "hg";
    private Context mContext;
    private IntentFilter mFilter;
    private OnHomePressedListener mListener;
    private InnerRecevier mRecevier;

    public HomeWatcher(Context context) {
        mContext = context;
        mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    }

    public void setOnHomePressedListener(OnHomePressedListener listener) {
        mListener = listener;
        mRecevier = new InnerRecevier();
    }

    public void startWatch() {
        if (mRecevier != null) {
            mContext.registerReceiver(mRecevier, mFilter);
        }
    }

    public void stopWatch() {
        if (mRecevier != null) {
            mContext.unregisterReceiver(mRecevier);
        }
    }

    class InnerRecevier extends BroadcastReceiver {
        final String SYSTEM_DIALOG_REASON_KEY = "reason";
        final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
        final String SYSTEM_DIALOG_REASON_LONG_PRESS = "assist";
        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
                if (reason != null) {
                    Log.e(TAG, "action:" + action + ",reason:" + reason);
                    if (mListener != null) {
                        if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
                            mListener.onHomePressed();
                        } else if (reason.equals(SYSTEM_DIALOG_REASON_LONG_PRESS)) {
                            mListener.onHomeLongPressed();
                        }
                    }
                }
            }
        }
    }
}



public interface OnHomePressedListener {
    public void onHomePressed();

    public void onHomeLongPressed();
}
Ninetta answered 14/10, 2016 at 3:9 Comment(1)
I can listen to home key only if I have a home screen activity ("android.intent.category.HOME") and also your answer is a copy paste of 1st answer of this question : #22953294 you should share the answers link to give credit instead of copy paste. I can not accept that answer.Boggart

© 2022 - 2024 — McMap. All rights reserved.