b85411
b85411

Reputation: 9990

Disable access to activity in Android

I have a settings menu. I want some users to be able to access one particular page on this menu (all users have to log in, some have admin access some only have user access) - only the admin users.

I have figured out how to remove this particular page from the menu if the user is not an admin.

But I consider it good practice to also add a check on the actual activity (java) page as well. In theory, with the link gone in the menu it should be impossible to access this page - but as I said I think it's good practice to add an additional check.

How can I simply "disable" an activity (grey screen, nothing can be touched etc besides the back button) in java code?

Upvotes: 0

Views: 296

Answers (1)

stkent
stkent

Reputation: 20128

A simple method is to override dispatchTouchEvent in your Activity. For example:

@Override
public boolean dispatchTouchEvent(@NonNull MotionEvent event) {
    if (shouldBlockTouchEvents) {
        return true;
    } else {
        return super.dispatchTouchEvent(event);
    }
}

This makes sure the default dispatching occurs when you are not deliberately blocking touch events.

Upvotes: 1

Related Questions