1

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

AD Tee
  • 345
  • 4
  • 21

1 Answers1

1

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
}
Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43
  • I tried to apply yr suggestion but getting error "Unresolved reference: sectionNumber". Any idea? – AD Tee Aug 14 '19 at 05:49
  • I did a silly mistake. I corrected by answer. Use this line `val sectionNumber = arguments?.getInt(ARG_SECTION_NUMBER) ?: 1` – Birju Vachhani Aug 14 '19 at 05:50
  • Hi Birju, after applied your code, both tabs will show same result. May I know which part I did wrong? You may refer to screen as attached above. – AD Tee Aug 14 '19 at 06:00
  • 1
    Finally its work when I change `if (sectionNumber == 0) {` to `if (sectionNumber == 1) {`. – AD Tee Aug 14 '19 at 08:13