簡體   English   中英

Kotlin:使用 Kotlin 提供的標准底部導航活動時如何將數據從 MainActivity 傳遞到片段

[英]Kotlin: How to pass data from the MainActivity to a fragment when working with the standard bottom navigation activity provided by Kotlin

Kotlin:當使用 Kotlin 提供的標准底部導航活動時,如何將數據從 MainActivity 傳遞到片段。

我怎樣才能將字符串“Hello world”(MainActivity.kt)傳遞給片段,我將能夠在這個片段中使用這個字符串。

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"

    }

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>

片段 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()
    }
    
}

如果您使用 SharedViewModel 這可以解決

請檢查此代碼實驗室示例

https://developer.android.com/codelabs/basic-android-kotlin-training-shared-viewmodel#4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM