Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

755
Views
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().

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

  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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!