How to open a new activity with conditions/parameters from recyclerview items?

Hi, I have the basic wireframe layout, who describe my question, because i don’t know how to explain with good typing. (but I’ll try to explain as clearly as possible what I mean and as much as I can).

*I hope you all can understand what my wireframe means :slightly_smiling_face:
*if you feel difficult to understand this, please tell me, I will make it better for more detail.

so here from the wireframe, i have 3 activity.

  1. MainActivity, with TabLayout, which is filled with Fragment and there is a RecyclerView in it,
  2. DetailActivity, which contains data brought from each RecyclerView Item from MainActivity ,
  3. WebViewActivity, which will be the last activity after the user clicks one of the buttons from DetaiActivity with a different context from each RecyclerView Item previously selected by user.

and from the wireframe I described, i’ve successfully open “WebViewActivity” and load the URL according to the specified parameters (google.com) from “DetailActivity” using “Button No.2”

My questions is :

  1. How To Open WebViewActivity and Load “Youtube.com”, When User Comes From “RecyclerView Item No.2” by clicking “Button 1” Inside of Detail Activity?.
  2. How To Open WebViewActivity and Load “Gmail.com”, When User Comes From “RecyclerView Item No.3” by clicking “Button 1” Inside of Detail Activity?.

This is my unresolved code :

MainActivity.kt

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        setUpTabs() // Setup TabLayout


    } // End - OnCreate


    // Start - TabLayout
    private fun setUpTabs() {
        val adapter = ViewPagerAdapter(supportFragmentManager)
        adapter.addFragment(MainTabFragment(), "MainTab")
        viewPager_tabLayout.adapter = adapter
        tabs.setupWithViewPager(viewPager_tabLayout)
    } // End - TabLayout


} // End - Class

DetailActivity.kt

class DetailActivity : AppCompatActivity() {

    var prodBundle: Bundle? = null // Initialize getString (bundle) from MainActivity
    private var urls = arrayOfNulls<String>(3) // Initialize URL Direction

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_single_product)


        // Start - WebView URL to WebViewActivity
        urls[0] = "google.com"
        urls[1] = "youtube.com"
        urls[2] = "gmail.com"
        // End - WebView URL to WebViewActivity


        initView() // Setup getString from RecyclerView Main Activity


    } // End - OnCreate


    // Start - getString from RecyclerView Main Activity
    fun initView() {
        prodBundle = intent.extras

        ph_titleText.text = prodBundle?.getString("TitleText")
       ph_descText.text = prodBundle?.getString("DescText")
        prodBundle?.getString("ImageURL").let {
            Glide.with(this).asBitmap()
                .load(it)
                .into(ph_urlImage)

            Glide.with(this).asBitmap()
                .load(it)
                .centerCrop()
                .fitCenter()
                .placeholder(R.drawable.ph_img_none)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
        }
    } //  End  - getString from RecyclerView Main Activity

    
    // Start - putExtra to WebViewActivity
    fun button2(view: View) {
        val i = Intent(this@DetailActivity, WebViewActivity::class.java)
        i.putExtra("linksWeView", urls[0])
        startActivity(i)
    } // End - putExtra to WebViewActivity

} // End - Class

WebViewActivity.kt

class WebViewActivity : AppCompatActivity() {


    private lateinit var webView: WebView // Initialize WebView


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_webview)


        // Start - Assign getString URL from DetailActivity
        val intent = intent
        val webSite = intent.getStringExtra("linksWebView")
        // End - Assign getString URL from DetailActivity


        // Start - WebView
        webView = findViewById(R.id.wv)

        webView.setWebViewClient(object : WebViewClient() {
            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
                view?.visibility = View.INVISIBLE

                if (Build.VERSION.SDK_INT >= 19) {
                    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null)
                } else {
                    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
                }
            }

            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                view?.visibility = View.VISIBLE
            }

            // Start - Fix SocialMedia Acces
            override fun shouldOverrideUrlLoading(wv: WebView, url: String): Boolean {
                if (url.startsWith("tel:") || url.startsWith("whatsapp:")) {
                    val intent = Intent(Intent.ACTION_VIEW)
                    intent.data = Uri.parse(url)
                    startActivity(intent)
                    webView.goBack()
                    return true
                }
                return false
            } // End - Fix SocialMedia Acces

        }) // End - WebView


        // Start - WebView Setting
        if (webSite != null) {
            webView.loadUrl(webSite)
        }

        val webSettings = webView.settings
        webSettings.javaScriptEnabled = true // js active
        webSettings.domStorageEnabled = true // componenet load
        webSettings.allowContentAccess = true
        webSettings.loadsImagesAutomatically = true
        webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH)
        webSettings.setEnableSmoothTransition(true)
        // End - WebView Setting


}// End - OnCreate

  
} // End - Class

MainTabFragment.kt

class MainTabFragment : Fragment() {


    lateinit var maintabfragmentAdapter: MainTabFragmentAdapter  // Initialize Adapter
    private val sLM = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL) // Initialize layoutManager
    val addMainTabFragmentModelList: MutableList<MainTabFragmentModel> = ArrayList() // Initialize listModel
    private var urlMainTabFragment = arrayOfNulls<String>(2) // Initialize URL Direction WebView

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_maintab, container, false)


    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // Start - Webview URL to WebViewActivity
        urlMainTabFragment[0] = "https://url1.com"
        urlMainTabFragment[1] = "https://url2.com"
        // End - Webview URL to WebViewActivity


        initViewMainTabFragment() // SetUp initView listModel
        actionMainTabFragment() // SetUp actionMainTabFragment
        iniRefreshListener() // SetUp swipeRefresh


    } // End - OnCreate


    // Start - intView listModel
    private fun initViewMainTabFragment() {
        rv_maintabfragment.layoutManager = sLM
        maintabfragmentAdapter = MainTabFragmentAdapter(requireActivity())
        rv_maintabfragment.adapter = maintabfragmentAdapter

        addMainTabFragmentModelList.add(
            MainTabFragmentModel("https://url1.com/img1.jpg", "TitleText#01", "DescText#01"))
        addMainTabFragmentModelList.add(
           MainTabFragmentModel("https://url1.com/img1.jpg", "TitleText#02", "DescText#02"))
        addMainTabFragmentModelList.add(
            MainTabFragmentModel("https://url1.com/img1.jpg", "TitleText#03", "DescText#03"))
        addMainTabFragmentModelList.add(
            MainTabFragmentModel("https://url1.com/img1.jpg", "TitleText#04", "DescText#04"))

        maintabfragmentAdapter.setMainTabFragment(addMainTabFragmentModelList)
    } // End - intView listModel


    // Start - putExtra to DetailActivity
    fun actionMainTabFragment() {
        maintabfragmentAdapter.setOnClickItemListenerMainTabFragment(object : OnItemClickListener {
            override fun onItemClick(item: View, position: Int) {
                lateinit var i: Intent
                if (position == 0) {
                    i = Intent(context, WebViewActivity::class.java)
                    i.putExtra("linksWebView", urlMainTabFragment[0])
                } else if (position == 3) {
                    i = Intent(context, WebViewActivity::class.java)
                    i.putExtra("linksWebView", urlMainTabFragment[1])
                } else {
                    i = Intent(context, DetailActivity::class.java)
                }
                i.putExtra("TitleText",
                    maintabfragmentAdapter.getMainTabFragment().get(position).titleTtextMainTab
                )
                i.putExtra(
                    "DescText",
                    maintabfragmentAdapter.getMainTabFragment().get(position).descTextMainTab
                )
                i.putExtra(
                    "ImageURL",
                    maintabfragmentAdapter.getMainTabFragment().get(position).imgMainTab
                )
                startActivity(i)
            }
        })
    } // End - putExtra to DetailActivity


} // End - Class

MainTabModel.kt

data class MainTabModel (var imgMainTab: String?, var titleTtextMainTab: String?, var descTextMainTab: String?)

OnItemClickListener.kt

interface OnItemClickListener {
    fun onItemClick(item: View, position:Int)
}

MianTabAdapter.kt

class MainTabAdapter(private val context: Context) :
    RecyclerView.Adapter<MainTabAdapter.MainTabViewHolder>() {

    private val MainTabModel: MutableList<MainTabModel> = mutableListOf()  // Initialize listModel
    private lateinit var onSelectedListenerMainTab: OnItemClickListener // Initialize onItemListener


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainTabViewHolder {
        return MainTabViewHolder(
            LayoutInflater.from(context).inflate(R.layout.item_maintab_layout, parent, false)
        )
    } // End - onCreateViewHolder

    
    override fun getItemCount(): Int {
        return MainTabModel.size
    }  // End - getItem

    
    override fun onBindViewHolder(holder: MainTabViewHolder, position: Int) {
        holder.maintabBindView(MainTabModel[position])
    } // End - onBindViewHolder

    
    // Start - setMainTab
    fun setMainTab(data: List<MainTabModel>) {
        MainTabModel.clear()
        MainTabModel.addAll(data)
        notifyDataSetChanged()
    } // End - setMainTab

    
    // Start - getMainTab
    fun getMainTab(): MutableList<MainTabModel> {
        return MainTabModel
    } // End - getMainTab

    
    // Start - MainTabViewHolder
    inner class MainTabViewHolder(maintabview: View) : RecyclerView.ViewHolder(maintabview) {
        val imgProdMainTab = maintabview.findViewById<ImageView>(R.id.iv_prodMainTab)
        val cvMainTab: MaterialCardView = maintabview.findViewById(R.id.cv_maintab)
        
        fun maintabBindView(MainTabModel: MainTabModel) {
            MainTabModel.apply {
                val image = MainTabModel.prodImgMainTab
                image.let {
                    Glide.with(itemView.context).asBitmap()
                        .load(it)
                        .into(imgProdMainTab)
                    
                    Glide.with(itemView.context).asBitmap()
                        .load(it)
                        .centerCrop()
                        .fitCenter()
                        .placeholder(R.drawable.ph_img_none)
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .skipMemoryCache(true)
                        .transition(BitmapTransitionOptions.withCrossFade())
                }
            }
        }
        
        // Start - onClick
        init {
            cvMainTab.setOnClickListener { onSelectedListenerMainTab.onItemClick(it, layoutPosition) }
        } // End - onClick
        
    } // End - MainTabBindViewHolder


    // Start - OnItemListener
    fun setOnClickItemListenerMainTab(onItemClickListener: OnItemClickListener) {
        this.onSelectedListenerMainTab = onItemClickListener
    } // End - OnItemListener


} // End - Class

I hope my explanation doesn’t make you confused, because I’m not too sure myself whether my question and wireframe can explain all what I mean. So, Hopefully someone help me solve this problem.

Thank you so much for taking your time which will definitely take a lot of effort to help me solve this problem. :slight_smile: :pray:

2022-07-27T17:00:00Z