簡體   English   中英

無法通過 navController 的 setGraph 中的包將數據從一個片段傳遞到另一個片段

[英]Can't pass data from one fragment to another through bundle in setGraph of navController

我將在片段之間傳遞數據; 我通過使用 bundle 並將其放入 setGraph 參數來執行此操作,但我無法在另一個片段中獲取它,並且它總是返回 null。 問題是什么?

片段導航

Bundle bundle = new Bundle();
bundle.putString("deviceSerialNo", "abc");
Log.d("TAGTAG", "onClick: " + bundle);
navController.setGraph(R.navigation.nav_graph, bundle);
navController.navigate(R.id.action_FirstFragment_to_SecondFragment);

第二個片段

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        Log.d("TAGTAG", "onCreateView: " + getArguments());
        if (getArguments() != null) {
            String string = getArguments().getString("deviceSerialNo");
        }


        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

根據setGraph()文檔,您傳入的Bundle稱為startDestinationArgs

startDestinationArgs : arguments 發送到圖的起始目的地

因此,使用帶有BundlesetGraph的唯一原因是,如果您的FirstFragment需要接收 arguments 的bundle 作為navigate()調用的一部分,您需要手動將這些 arguments 傳遞給任何其他片段,例如SecondFragment

// Pass the arguments to the SecondFragment
navController.navigate(R.id.action_FirstFragment_to_SecondFragment, bundle);

暫無
暫無

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

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