0

Im currently working on an app, that has a BottomSheet as a menu. The objective of this menu is to start intents depending on the item selected. I tried starting an Intent like below, however Android Studio says:

None of the following functions can be called with the arguments supplied.

  • (Context!, Class<*>!) defined in android.content.Intent

  • (String!, Uri!) defined in android.content.Intent

What am i doing wrong? Is there a better way to start an Intent from a class?

frgBottomSheetDrawer.kt

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)

        navDrawer.setNavigationItemSelectedListener { menuItem ->
            when (menuItem!!.itemId) {
                R.id.ndListFolder -> Intent(this, ndActFolder::class.java).also {
                    startActivity(it) 
                }
                R.id.ndListSettings -> Intent(this, ndActSettings::class.java).also {
                    startActivity(it) 
                }
                true
            }
        }

        return inflater.inflate(R.layout.fragment_bottomsheet, container, false)
    }
Meltix
  • 436
  • 6
  • 22

2 Answers2

1

You can listen for events in bottom sheet from your activity by creating custom listener. You could do something like this:

In your BottomSheet:

var mListener: BottomSheetListener? = null

interface BottomSheetListener{
    fun onEventHappened(foo: Foo)
}

// Attach activity to your listener
override fun onAttach(context: Context) {
    super.onAttach(context)
    mListener = context as BottomSheetListener
}

In your Activity:

class MainActivity : AppCompatActivity(), BottomSheet.BottomSheetListener

It will want you to override onEventHappened method.

When you want to nagivate from your bottom sheet, run mListener.onEventHappened(foo) line in your BottomSheet class. It will trigger the onEventHappened() method in your Activity. Then you can start intents by your activity context.

Teyyihan Aksu
  • 156
  • 2
  • 10
  • Thanks for the answer, i'll give that a try. Are there any other ways to manage this? Like, directly inside of the fragment, or something else? – Meltix Jul 04 '20 at 16:29
  • I found a cleaner solution, check the updated question. Thanks again for the answer! – Meltix Jul 04 '20 at 17:21
0

I found a clean solution.

We can write a cleaner Intent line:

this.startActivity(Intent(activity, actAbout::class.java))

In case you need the activity normally, we can write:

new intent = Intent(activity, actAbout::class.java))
startActivity(intent)
Meltix
  • 436
  • 6
  • 22