Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

java - Android: How to pass parameters to Fragment in Bottom Navigation Activity

I'm developing Java application which has an Activity with three Fragments and Bottom Navigation bar. It was generated automatically by Android Studio (Right Click -> New -> Activity -> Bottom Navigation Activity). I need to pass one String parameter to all slave fragments.

I've dug through some Stack Overflow articles but I haven't found what I'm looking for.

Thew activity class looks like that. I've added to defalt code a Bundle which keeps my parameter, an argument builder and the navGraph which is then added to the NavController.
https://github.com/SP8EBC/Pogoda.cc/blob/master/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/TrendActivity.java

public class TrendActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String stationName = (String)getIntent().getExtras().get("station");
        setContentView(R.layout.activity_trend);

        NavArgument.Builder builder = new NavArgument.Builder();

        Bundle bundle = new Bundle();
        bundle.putString("station", stationName);

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_pressure, R.id.navigation_temperature, R.id.navigation_wind)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavGraph navGraph = navController.getNavInflater().inflate(R.navigation.mobile_navigation);
        builder.setDefaultValue(stationName);
        navGraph.addArgument("station", builder.build());
        navController.setGraph(navGraph, bundle);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
//
//        NavArgument.Builder builder = new NavArgument.Builder();
//        builder.setDefaultValue(stationName);
//        navGraph.addArgument("station", builder.build());
//       // navController.get
//        navController.setGraph(navGraph);

    }

}

Fragment code looks like that https://github.com/SP8EBC/Pogoda.cc/blob/master/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/trend/wind/WindTrendFragment.java

public class WindTrendFragment extends Fragment {

    private WindTrendViewModel windTrendViewModel;

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

        String station = "";

        windTrendViewModel =
                new ViewModelProvider(this).get(WindTrendViewModel.class);
        View root = inflater.inflate(R.layout.fragment_wind, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        windTrendViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        Bundle arg = this.getArguments();

        if (arg != null) {
            station = arg.getString("station");
        }

        return root;
    }
}

And now what problem I still have. Such construction works only for WindTrendFragment which is the default one (one the Navigation activity shows when launched) but in strange way. It looks like this fragment is created twice (onCreateView is called twice). The first time an execution stops on a breakpoint this.getArguments(); returns null, but the second time I've got my bundle with an argument. Main problem is that it works only for this fragment. When I switch to another one using bottom nav bar I always get null when I try to get arguments.

question from:https://stackoverflow.com/questions/66065018/android-how-to-pass-parameters-to-fragment-in-bottom-navigation-activity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...