Home > other >  Share web url from browser to navigation fragment
Share web url from browser to navigation fragment

Time:10-17

In my app I had only one activity and 3 fragments(mainF,saveF,updateF).

Main activity only set layout activity_main and this activity layout only set navHost Fragment. I want to send url from browser to one of the fragments, like browser ->share url ->fragment ->save.

Previously this app have two activities, so I used intent filters and get the url and stored in the view. But how to share it directly in the fragments in nav graph.

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

    if (intent?.action == Intent.ACTION_SEND) {
        if ("text/plain" == intent.type) {
            intent.getStringExtra(Intent.EXTRA_TEXT)?.let {

                val navController = findNavController(R.id.navHostFragment)
                val bundle = Bundle()
                bundle.putString("urlFromWeb",it)
                navController.navigate(R.id.saveFragment,bundle)
            }
        }
    }
}

}

my activity_main xml

<androidx.constraintlayout.widget.ConstraintLayout 
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewModel.MainActivity">

<androidx.fragment.app.FragmentContainerView
    android:id="@ id/navHostFragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/main_navigation_file">


</androidx.fragment.app.FragmentContainerView>

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

You can receive that data in the Main Activity just like before and open the required navigation fragment bypassing that received data with it.

CodePudding user response:

We can not directly access nav contoller from our activity .So this method is used.

Main acitivity

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

    if (intent?.action == Intent.ACTION_SEND) {
        if ("text/plain" == intent.type) {
            intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
               val navHost = supportFragmentManager.findFragmentById(R.id.navHostFragment) as NavHostFragment
                val navContoller = navHost.findNavController()
                val bundle = Bundle()
                bundle.putString("url",it)
                navContoller.navigate(R.id.saveFragment,bundle)
            }
        }
    }
}

}

saveFragment where intent will be used

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val view = inflater.inflate(R.layout.fragment_save, container, false)
    // Getting the text that is sent from the MainActivity
    val message = arguments?.getString("url")

    titleEdit = view.findViewById(R.id.bookmark_title)
    urlEdit = view.findViewById(R.id.bookmark_url)
    descripEdit = view.findViewById(R.id.description_EditText)!!
    saveBtn = view.findViewById(R.id.savebtn)
    
    // Using that text in our view
    if (message != null) {
            urlEdit.setText(message)
    }
  • Related