Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startActivity() vs context.startActivity()

I started android development a few days ago. I implemented a recylerview and in the OnBindViewHolder method of the recyclerview adapter I used the setOnClickListener on the recyclerview item. My main goal was to start a new activity when the recyclerview item is clicked but I ran into a wall when implementing my code in the following way:

     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val clusterItem = datalist[position]
    holder.clusterName.setText(clusterItem.name)
    holder.clusterStrat.setText(clusterItem.strats)
    holder.itemview.setOnClickListener() { 
        startActivity(Intent(holder.itemview.context,ClusterSearchActivity::class.java))
    }
}

I had 3 errors on the line that contains startActivity:

Type mismatch: inferred type is Intent but Context was expected

No value passed for parameter 'intent'

No value passed for parameter 'options'

After going through multiple solutions I finally stumbled upon this one: https://www.titanwolf.org/Network/q/08ad14d9-cb9a-4b87-923b-f97089db769a/y

Using context.startActivity(intent) I rewrote my code like this:

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
         val clusterItem = datalist[position]
         holder.clusterName.setText(clusterItem.name)
         holder.clusterStrat.setText(clusterItem.strats)
         holder.itemview.setOnClickListener() {
    holder.itemview.context.startActivity(Intent(holder.itemview.context,ClusterSearchActivity::class.java)) }

}

Now my code finally worked but I can't seem to understand why I had to use context.startActivity(). I'd like to understand when can I use startActivity() just like that and when I need to use context.startActivity().

like image 899
Sarwar Khalid Avatar asked Jun 16 '26 08:06

Sarwar Khalid


2 Answers

  1. First of all, Context is the current state of the application/object.

  2. Interface to global information about an application environment.

  3. This is an abstract class whose implementation is provided by the Android system.

  4. It allows access to application-specific resources like colors, string resource , database access and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

  5. Context is the base class for Activity, Service, Application, etc if you check inside the AppCompatActivity and Fragment. then you can be found startActivity() method inside it.

In Your case:

In the adapter, If you need to get the database access , string res e.g: context.getResources().getString(R.string.yourstring);

needed color to set on the view on runtime , so you have to need the current state of the application/object is call context and context is a super class.

You can get access to context in three ways in the adapter.

  1. Pass Context as an argument to the Adapter and keep it as class field.

  2. Use dependency injection to inject Context when you need it. I strongly suggest reading about it.e.g: Android Hilt.

At last,

  1. Get it from any View object. just like you did it. holder.itemview.context.

I hope, it might be helpful for you.

like image 67
Muhammad Ammar Avatar answered Jun 17 '26 23:06

Muhammad Ammar


That's because the method startActivity() is a member of the Activity class and not RecyclerView, it simply does not exist there.

Incidentally, a better way that you can start an Activity from a RecyclerView click is via a callback.

like image 25
Ivan Wooll Avatar answered Jun 17 '26 23:06

Ivan Wooll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!