androidkotlinfragmentandroid-tabbed-activity

How can I change different URL or view in tabbed activity?


I'm new in Android kotlin development. I would like to create a tabbed activity in my app to show different static HTML page.

I able to set 1 URL in my app now. But when I change or tap to another tab, it will show similar view or URL.


import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.support.v4.app.Fragment
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.webkit.WebView
import xxx.abcabc.xxx.R
import android.util.Log


class PlaceholderFragment : Fragment() {

    private lateinit var pageViewModel: PageViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        pageViewModel = ViewModelProviders.of(this).get(PageViewModel::class.java).apply {
            setIndex(arguments?.getInt(ARG_SECTION_NUMBER) ?: 1)
        }


    }

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_left_menu_policies, container, false)

        var webTnc: WebView = root.findViewById(R.id.myWebView)
        webTnc.loadUrl("https://www.abcxyz.com/abcxyz/abcxyz_webpage/tnc.html")


        return root
    }

    companion object {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private const val ARG_SECTION_NUMBER = "section_number"

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        @JvmStatic
        fun newInstance(sectionNumber: Int): PlaceholderFragment {
            return PlaceholderFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_SECTION_NUMBER, sectionNumber)
                }
            }
        }

    }
}

How can I detect sectionNumber something like

if(sectionNumber == 0)
{
 var webTnc: WebView = root.findViewById(R.id.myWebView)
    webTnc.loadUrl("https://www.abcxyz.com/abcxyz/abcxyz_webpage/tnc.html")
}
else
{
var webTnc: WebView = root.findViewById(R.id.myWebView)
    webTnc.loadUrl("https://www.URL2.com/URL2.html")
}

Please help. Thank you.

Edited enter image description here


Solution

  • I see that you're putting sectionNumber in bundle arguments in fragment. So you can use that arguments to get the data back.

    Try this:

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_left_menu_policies, container, false)
        val sectionNumber = arguments?.getInt(ARG_SECTION_NUMBER) ?: 1
        if (sectionNumber == 1) {
            var webTnc: WebView = root.findViewById(R.id.myWebView)
            webTnc.loadUrl("https://www.abcxyz.com/abcxyz/abcxyz_webpage/tnc.html")
        } else {
            var webTnc: WebView = root.findViewById(R.id.myWebView)
            webTnc.loadUrl("https://www.URL2.com/URL2.html")
        }
    
    
        return root
    }