Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference registerForContextMenu and setOnCreateContextMenuListener?

Tags:

android

menu

what's the difference between registerForContextMenu :

Registers a context menu to be shown for the given view (multiple views can show the context menu). This method will set the View.OnCreateContextMenuListener on the view to this activity

Call registerForContextMenu() and pass it the View you want to give a context menu. When this View then receives a long-press, it displays a context menu.

and setOnCreateContextMenuListener :

Register a callback to be invoked when the context menu for this view is being built. If this view is not long clickable, it becomes long clickable.

which one to use? and about the long clickable stuff : both are doing the same thing...

Thanks

like image 358
Paul Avatar asked Dec 28 '11 03:12

Paul


People also ask

What is registerForContextMenu?

In Activity class, there is method called registerForContextMenu(View view) . The android document explains that this method is used to registers a context menu to be shown for the given view (multiple views can show the context menu).

What is context menu android?

In Android, the context menu is like a floating menu and arises when the user has long pressed or clicks on an item and is beneficial for implementing functions that define the specific content or reference frame effect. The Android context menu is alike to the right-click menu in Windows or Linux.


1 Answers

When in doubt... look at the Android source code! It is open source, after all. :)

git://android.git.kernel.org/platform/frameworks/base.git/core/java/android/view/View.java:

/**
 * Register a callback to be invoked when the context menu for this view is
 * being built. If this view is not long clickable, it becomes long clickable.
 *
 * @param l The callback that will run
 *
 */
public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
    if (!isLongClickable()) {
        setLongClickable(true);
    }
    mOnCreateContextMenuListener = l;
}

git://android.git.kernel.org/platform/frameworks/base.git/core/java/android/app/Activity.java:

/**
 * Registers a context menu to be shown for the given view (multiple views
 * can show the context menu). This method will set the
 * {@link OnCreateContextMenuListener} on the view to this activity, so
 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
 * called when it is time to show the context menu.
 *
 * @see #unregisterForContextMenu(View)
 * @param view The view that should show a context menu.
 */
public void registerForContextMenu(View view) {
    view.setOnCreateContextMenuListener(this);
}

So, the answer is that they're the same. registerForContextMenu() does nothing except invoke setOnCreateContextMenuListener().

like image 92
Trevor Johns Avatar answered Oct 11 '22 03:10

Trevor Johns