Android Studio actionProviderClass issue

Viewed 33
   <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ankidroid="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@+id/action_undo"
        android:enabled="true"
        android:icon="@drawable/ic_undo_white"
        android:title="@string/undo"
        ankidroid:actionProviderClass="com.ichi2.ui.RtlCompliantActionProvider"
        ankidroid:showAsAction="always"/>
    <item
        android:id="@+id/action_rebuild"
        android:title="@string/rebuild_cram_label"
        android:visibility = "gone"
        android:icon="@drawable/ic_refresh_white"
        ankidroid:showAsAction="ifRoom"/>
    <item
        android:id="@+id/action_empty"
        android:title="@string/empty_cram_label"
        android:visibility = "gone"
        android:icon="@drawable/ic_clear_white"
        ankidroid:showAsAction="ifRoom"/>
    <item
        android:id="@+id/action_custom_study"
        android:title="@string/custom_study"
        android:icon="@drawable/ic_build_white"
        ankidroid:showAsAction="ifRoom"/>
    <item
        android:id="@+id/action_rename"
        android:title="@string/rename_deck"
        android:visibility = "gone"/>
    <!-- the title for the following option will be set dynamically -->
    <item
        android:id="@+id/action_deck_or_study_options"
        android:title="@string/menu__deck_options"
        android:icon="@drawable/ic_tune_white"
        ankidroid:showAsAction="always"/>
    <item
        android:id="@+id/action_delete"
        android:title="@string/contextmenu_deckpicker_delete_deck"
        android:visibility = "gone"/>
    <item
        android:id="@+id/action_export"
        android:title="@string/export_deck"
        android:visibility = "gone"/>
    <item
        android:id="@+id/action_unbury"
        android:title="@string/unbury"
        android:visibility = "gone"/>
</menu>

This is the xml file and below is the action provider class used in action_undo item/button. So basically the undo_action button is not working in the fragment when set on item click

class RtlCompliantActionProvider(context: Context) : ActionProviderCompat(context) {
    @JvmField
    @VisibleForTesting
    val mActivity: Activity

    override fun onCreateActionView(forItem: MenuItem): View {
        val actionView = ImageButton(context, null, R.attr.actionButtonStyle)
        TooltipCompat.setTooltipText(actionView, forItem.title)
        val iconDrawable = forItem.icon
        iconDrawable.isAutoMirrored = true
        actionView.setImageDrawable(iconDrawable)
        actionView.id = R.id.action_undo
        actionView.setOnClickListener {
            if (!forItem.isEnabled) {
                return@setOnClickListener
            }
            mActivity.onOptionsItemSelected(forItem)
        }
        return actionView
    }

    companion object {
        /**
         * Unwrap a context to get the base activity back.
         * @param context a context that may be of type [ContextWrapper]
         * @return The activity of the passed context
         */
        private fun unwrapContext(context: Context): Activity {
            var unwrappedContext: Context? = context
            while (unwrappedContext !is Activity && unwrappedContext is ContextWrapper) {
                unwrappedContext = unwrappedContext.baseContext
            }
            return if (unwrappedContext is Activity) {
                unwrappedContext
            } else {
                throw ClassCastException("Passed context should be either an instanceof Activity or a ContextWrapper wrapping an Activity")
            }
        }
    }

    init {
        mActivity = unwrapContext(context)
    }
}


this is the fragment in which the option click listener is set

      override fun onMenuItemClick(item: MenuItem): Boolean {
            when (item.itemId) {
                R.id.action_undo -> {
                    Timber.i("StudyOptionsFragment:: Undo button pressed")
                    if (BackendFactory.defaultLegacySchema) {
                        Undo().runWithHandler(mUndoListener)
                    } else {
                        launchCatchingTask {
                            if (requireActivity().backendUndoAndShowPopup()) {
                                openReviewer()
                            } else {
                                Undo().runWithHandler(mUndoListener)
                            }
                        }
                    }
                    return true
                }
                R.id.action_deck_or_study_options -> {
                    Timber.i("StudyOptionsFragment:: Deck or study options button pressed")
                    if (col!!.decks.isDyn(col!!.decks.selected())) {
                        openFilteredDeckOptions()
                    } else {
                        val i = Intent(activity, DeckOptions::class.java)
                        Timber.i("Opening deck options for activity result")
                        onDeckOptionsActivityResult.launch(i)
                        slide(requireActivity(), ActivityTransitionAnimation.Direction.FADE)
                    }
                    return true
                }
                R.id.action_custom_study -> {
                    Timber.i("StudyOptionsFragment:: custom study button pressed")
                    showCustomStudyContextMenu()
                    return true
                }
                R.id.action_unbury -> {
                    Timber.i("StudyOptionsFragment:: unbury button pressed")
                    col!!.sched.unburyCardsForDeck()
                    refreshInterfaceAndDecklist(true)
                    item.isVisible = false
                    return true
                }
                R.id.action_rebuild -> {
                    Timber.i("StudyOptionsFragment:: rebuild cram deck button pressed")
                    mProgressDialog = show(
                        requireActivity(), null,
                        resources.getString(R.string.rebuild_filtered_deck), true
                    )
                    TaskManager.launchCollectionTask(RebuildCram(), getCollectionTaskListener(true))
                    return true
                }
                R.id.action_empty -> {
                    Timber.i("StudyOptionsFragment:: empty cram deck button pressed")
                    mProgressDialog = show(
                        requireActivity(), null,
                        resources.getString(R.string.empty_filtered_deck), false
                    )
                    TaskManager.launchCollectionTask(EmptyCram(), 
  getCollectionTaskListener(true))
                    return true
                }
                R.id.action_rename -> {
                    (activity as DeckPicker).renameDeckDialog(col!!.decks.selected())
                    return true
                }
                R.id.action_delete -> {
                    (activity as DeckPicker).confirmDeckDeletion(col!!.decks.selected())
                    return true
                }
                R.id.action_export -> {
                    (activity as DeckPicker).exportDeck(col!!.decks.selected())
                    return true
                }
                else -> return false
            }
        }

All the other buttons are working fine even the undo action button is working fine if we remove the provider class but when we place the provider class in the button just stops working.

0 Answers
Related