Using navigation graph, when i navigate from fragment to activity and also passing argument using safeargs, in activity, I cannot get argument. How can I get argument passing from fragment???
In fragment, I can get argument by getArgument()
function, but not in activity.
In fragment I switch to another activity by:
findNavController().navigate(AFragmentDirections.actionAFragmentToBActivity(1)
and in B activity get argument in onCreate by :
val args = BActivityArgs.fromBundle(savedInstanceState!!)
but my app crash immediately.
Check it out the Android Doc :-
https://developer.android.com/guide/navigation/navigation-pass-data#java
Send Data
@Override
public void onClick(View view) {
EditText amountTv = (EditText) getView().findViewById(R.id.editTextAmount);
int amount = Integer.parseInt(amountTv.getText().toString());
ConfirmationAction action =
SpecifyAmountFragmentDirections.confirmationAction()
action.setAmount(amount)
Navigation.findNavController(view).navigate(action);
}
Get Data :-
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
TextView tv = view.findViewById(R.id.textViewAmount);
int amount = ConfirmationFragmentArgs.fromBundle(getArguments()).getAmount();
tv.setText(amount + "")
}