SafeArgs and Navigation Component to open url android studio

68 Views Asked by At

I'm currently want to open url if it's clicked when i'm passing data using navigation component, and its ben a few days since i'm trying to fix it and i don't know where is wrong with my code

the second fragment called fragment_barang cannot be clicked and i even use webview as a catalyst to open the web, can you guys help me with this?

this is fragment_barang

package rizkyfadilah.binar.synrgy6.android.challengechapter3

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.Toolbar
import androidx.navigation.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import rizkyfadilah.binar.synrgy6.android.challengechapter3.databinding.FragmentBarangBinding

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [fragment_barang.newInstance] factory method to
 * create an instance of this fragment.
 */
class fragment_barang : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null
    private var _binding: FragmentBarangBinding? = null
    private val binding get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }



    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        _binding = FragmentBarangBinding.inflate(inflater,container,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val barang1 =  arguments?.getString(Fragment_Abjad.EXTRA_BARANG1)
        val barang2 =arguments?.getString(Fragment_Abjad.EXTRA_BARANG2)
        val barang3 = arguments?.getString(Fragment_Abjad.EXTRA_BARANG3)
        val abjad = arguments?.getString(Fragment_Abjad.HURUF)
        val toolbar = view.findViewById<Toolbar>(R.id.t_toolbar_barang)

        toolbar.apply {
            title="Word Start With $abjad"
        }

        val listbarang = arrayListOf(
            barang(barang1 = "$barang1"),
            barang(barang1 ="$barang2"),
            barang(barang1 = "$barang3")
        )
        val rv_barang = view.findViewById<RecyclerView>(R.id.rv_barang)
        rv_barang.apply {
            layoutManager =LinearLayoutManager(activity)
            adapter = listadapterbarang(listbarang)

        }

    }


    companion object {
        val EXTRA_LINK = "EXTRA_LINK"
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment fragment_barang.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            fragment_barang().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }

}

this is webfragment

package rizkyfadilah.binar.synrgy6.android.challengechapter3

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [webfragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class webfragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_webfragment,container,false)
        val webview = view.findViewById<WebView>(R.id.webView)
        webview.webViewClient = WebViewClient()
        webview.settings.javaScriptEnabled = true
        val url = arguments?.getString("url")
        if (!url.isNullOrBlank()){

            webview.loadUrl(url)

        }
        return view

    }


    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment webfragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            webfragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

this is main_navigation

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_navigation"
    app:startDestination="@id/fragment_Abjad">

    <fragment
        android:id="@+id/fragment_Abjad"
        android:name="rizkyfadilah.binar.synrgy6.android.challengechapter3.Fragment_Abjad"
        android:label="fragment__abjad"
        tools:layout="@layout/fragment__abjad" >
        <action
            android:id="@+id/action_fragment_Abjad_to_fragment_barang"
            app:destination="@id/fragment_barang" />
        <action
            android:id="@+id/action_fragment_Abjad_to_fragment_barang2"
            app:destination="@id/fragment_barang" />
    </fragment>
    <fragment
        android:id="@+id/fragment_barang"
        android:name="rizkyfadilah.binar.synrgy6.android.challengechapter3.fragment_barang"
        android:label="fragment_barang"
        tools:layout="@layout/fragment_barang" >
        <argument
            android:name="barang1"
            app:argType="string"
            android:defaultValue="Kosong" />
        <argument android:name="barang2"
            app:argType="string"
            android:defaultValue="Kosong" />
        <argument
            android:name="barang3"
            app:argType="string"
            android:defaultValue="Kosong" />
        <action
            android:id="@+id/action_fragment_barang_to_webFragment"
            app:destination="@id/webFragment" />

    </fragment>
    <fragment
        android:id="@+id/webFragment"
        android:name="rizkyfadilah.binar.synrgy6.android.challengechapter3.webfragment"
        android:label="Web Fragment"
        android:layout = "@layout/fragment_webfragment">
        <argument
            android:name="url"
            app:argType="string" />
    </fragment>

</navigation>

and this is the layout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".fragment_barang">

    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_purple"
        android:id="@+id/t_toolbar_barang"/>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="80dp"
        android:id="@+id/rv_barang"
        />



</FrameLayout>
0

There are 0 best solutions below