passing data through navigation between fragments android studio
Asked Answered
S

2

2

I'm new to android studio and dont fully understand the working of it yet but I've been attempting to send data from one fragment to another in android studio. I've not been able to find a clear answer to this issue. What I'm trying to do is make a basic parking app where a qr code is scanned and the data that is in the qr code (parking name and price per hour) will be send to the next fragment since the qr code scanner fragment will open an addParkingSession fragment where I'll need the parking data. I've been trying to use "Navigation" to do this but i'm unable to find a way to send data via this methode. Is there a different mehode that better suits this (it has to go from fragment to fragment though) I've been trying to use intent but navigation doesn't seem to have an option to send an intent along with it.

Here is some of my code as an example. Thank you for your help and understanding

codeScanner.setDecodeCallback(new DecodeCallback() {
        @Override
        public void onDecoded(@NonNull Result result) {
            getActivity().runOnUiThread(new Runnable(){
                @Override
                public void run(){
                    if(result.getText() != null) {
                        String[] parkingdata = resultData.toString().split(",");
                        Intent intent = new Intent(getActivity().getBaseContext(), AddSession.class);
                        intent.putExtra("parkingName", parkingdata[0]);
                        intent.putExtra("parkingPrice", parkingdata[1]);
                        Navigation.findNavController(view).navigate(R.id.action_qrSession_to_addSession);
                    }
                }
            });
        }
    });
Sherrisherrie answered 27/11, 2021 at 14:36 Comment(1)
Please check out this link developer.android.com/guide/navigation/navigation-pass-dataBantam
A
4

You can pass a bundle object as the second argument in .navigate() and access it in your fragment with getArguments().

final Bundle bundle = new Bundle();
bundle.putString("test", "Hello World!");
Navigation.findNavController(view).navigate(R.id.action_qrSession_to_addSession, bundle);
public class MyFragment extends Fragment {
  
  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // the string you passed in .navigate()
    final String text = getArguments().getString("test");
  }
}
Arria answered 27/11, 2021 at 18:37 Comment(0)
G
0

Unless you are tackling a complex application, use the ViewModelProvider as suggested by Android.com here https://developer.android.com/guide/fragments/communicate.

getActivity() rather than 'this' will store you application's values at the MainActivity level instead of the object level so you will be able to retain/exchange any of your fragment variables with other activities or fragments. The IDE generates the view models and the code for them in each fragment's source when you use the fragment templates. You can directly access the variables from the ViewModelProvider and use gets and sets as your shop requires:

// Fragment A set
// getActivity() rather than 'this' for global scope
NotificationsViewModel notificationsViewModel = new ViewModelProvider(getActivity()).get(NotificationsViewModel.class);
notificationsViewModel.mUri = audioList.uris.get(position);

// Fragment B get
NotificationsViewModel notificationsViewModel = new ViewModelProvider(getActivity()).get(NotificationsViewModel.class);
HomeViewModel homeViewModel = new ViewModelProvider(getActivity()).get(HomeViewModel.class);
homeViewModel.mMediaPlayer.setDataSource(notificationsViewModel.mUri);

The view model is also used to retain values during rotations. So, if you have seen the view model generated source in your app and not sure what it is now you know!

Geisel answered 24/3 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.