Android Navigation passes in data when loading the default fragment for the first time

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

Today, when I use navigation to load a fragment, I want to transfer some data from the activity to the fragment when it is loaded for the first time. There is no relevant information on the Internet. I have studied the NavController.NavGraph.NavArgument of navigation, and finally found that these are all It’s not easy to use, just getActivity in the fragment and then call the variable. At that time, I wanted to scold myself for the previous research. Send it out and record it

Steps for usage

1. Define variables in Activity

The code is as follows (example):

    protected ArrayList<String> strings;
    strings = = new ArrayList<>();
    strings.add"QAQ";
    strings.add"AQA";
    strings.add"___";

2. Get data in fragment

The code is as follows (example):

     MyActivity activity = (MyActivity) getActivity();
     ArrayList<String> strings =  activity.strings;

Summarize

Because navigation has only been used recently, when I used it, I always thought about doing it according to the characteristics of navigation, but forgot the simplest way.

Guess you like

Origin blog.csdn.net/LLCCQAQ/article/details/131575261