javaandroidkotlinandroid-recyclerviewnestedrecyclerview

I am trying to open custom chrome tab from nested recycler view but couldn't having error Calling startActivity() from outside of an Activity


Here is the code I am using to open the custom chrome tab

val builder = CustomTabsIntent.Builder()
                val customTabsIntent = builder.build()
                customTabsIntent.launchUrl(context.applicationContext, Uri.parse("https://www.codingkaro.in"))

This is set on a button click listener inside nested recycler view. I have passed the context from the MainActivity as this@MainActivity

This is the error I am getting

Process: com.shyptsolution.classproject, PID: 31505
    android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Solution

  • Earlier I didn't know how to addFlags() to this but now the code looks like this

    val builder = CustomTabsIntent.Builder()
                    val customTabsIntent = builder.build()
                    customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    customTabsIntent.launchUrl(holder.download.rootView.context, Uri.parse("https://www.codingkaro.in"))
    

    and everything works fine. I just added customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)