Kotlin: How to pass data from the MainActivity to a fragment when working with the standard bottom navigation activity provided by Kotlin.
How could I pass the string "Hello world" (MainActivity.kt) to a fragment and I would be able to use this string in this fragment.
MainActivity.kt:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
/* */
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navView: BottomNavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_activity_main)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home,
R.id.navigation_messages,
R.id.navigation_additem,
R.id.navigation_graphics,
R.id.navigation_account
)
)
var value = ""
val extras = intent.extras
if (extras != null) {
value = extras.getString("accountid").toString()
}
Toast.makeText(this, value, Toast.LENGTH_SHORT)
.show()
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
var string = "Hello world"
}
A part of mobile_navigation.xml:
<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/mobile_navigation"
app:startDestination="@+id/navigation_home">
<fragment
android:id="@+id/navigation_home"
android:name="com.example.yourstuff.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_navigation_home_to_navigation_graphics"
app:destination="@id/navigation_graphics" />
<action
android:id="@+id/action_navigation_home_to_navigation_account"
app:destination="@id/navigation_account" />
<action
android:id="@+id/action_navigation_home_to_navigation_messages"
app:destination="@id/navigation_messages" />
<action
android:id="@+id/action_navigation_home_to_navigation_additem"
app:destination="@id/navigation_additem" />
</fragment>
<fragment
android:id="@+id/navigation_messages"
android:name="com.example.yourstuff.ui.messages.MessagesFragment"
android:label="@string/title_messages"
tools:layout="@layout/fragment_messages" >
<action
android:id="@+id/action_navigation_messages_to_navigation_home"
app:destination="@id/navigation_home" />
<action
android:id="@+id/action_navigation_messages_to_navigation_graphics"
app:destination="@id/navigation_graphics" />
<action
android:id="@+id/action_navigation_messages_to_navigation_account"
app:destination="@id/navigation_account" />
<action
android:id="@+id/action_navigation_messages_to_navigation_additem"
app:destination="@id/navigation_additem" />
</fragment>
Example of a fragment HomeFragment:
class HomeFragment : Fragment(), ItemClick {
private lateinit var homeViewModel: HomeViewModel
private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java)
_binding = FragmentHomeBinding.inflate(inflater, container, false)
val root: View = binding.root
binding.gridRecyclerView.adapter = ItemCardAdapter(
activity,
Layout.GRID,
this
)
// Specify fixed size to improve performance
binding.gridRecyclerView.setHasFixedSize(true)
return root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
override fun onClick(position: Int, name: String, description: String) {
Toast.makeText(getActivity(), position.toString() + name + description, Toast.LENGTH_SHORT)
.show()
}
}
This can resolve if you use SharedViewModel
please check this code lab example
https://developer.android.com/codelabs/basic-android-kotlin-training-shared-viewmodel#4
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.