Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Optimized code for opening PlantDetailActivity #21

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package com.google.samples.apps.sunflower

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Bundle.EMPTY
import android.support.v4.app.ActivityCompat
import android.support.v7.app.AppCompatActivity

/**
Expand Down Expand Up @@ -44,4 +48,14 @@ class PlantDetailActivity : AppCompatActivity() {
onBackPressed()
return true
}

companion object {
fun showInstance(cxt: Context, plantId: String) {
with(Intent(cxt, PlantDetailActivity::class.java)) {
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
putExtra(PlantDetailFragment.ARG_ITEM_ID, plantId)
ActivityCompat.startActivity(cxt, this, EMPTY)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.samples.apps.sunflower.adapters

import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
Expand All @@ -26,7 +25,6 @@ import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.google.samples.apps.sunflower.PlantDetailActivity
import com.google.samples.apps.sunflower.PlantDetailFragment
import com.google.samples.apps.sunflower.PlantListFragment
import com.google.samples.apps.sunflower.R
import com.google.samples.apps.sunflower.data.Plant
Expand All @@ -44,10 +42,7 @@ class PlantAdapter : RecyclerView.Adapter<PlantAdapter.ViewHolder>() {

private val onClickListener = View.OnClickListener { view ->
val item = view.tag as Plant
val intent = Intent(view.context, PlantDetailActivity::class.java).apply {
putExtra(PlantDetailFragment.ARG_ITEM_ID, item.plantId)
}
view.context.startActivity(intent)
PlantDetailActivity.showInstance(view.context, item.plantId)
}

override fun getItemCount() = values.size
Expand All @@ -71,6 +66,11 @@ class PlantAdapter : RecyclerView.Adapter<PlantAdapter.ViewHolder>() {
R.layout.list_item_plant, parent, false))
}

override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder)
holder.itemView.tag = null
}

/**
* Use this constructor to create a new ViewHolder.
*
Expand Down