How to return data from a fragment using androidx.navigation?

Viewed 43

I open a selection fragment using androidx.navigation, but I cannot find a way to return data to the previous fragment.

1 Answers

In one parent activity.. just use global variable that able to access inside same parent activity or

Other trick you can try preference, so that preference will able to read anywhere you want.

Data share active fragment

sharedPref =  context.getSharedPreferences(context.getPackageName(),MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString(sKey, sValue);
prefEditor.apply();

Data read from other fragment

sharedPref =  context.getSharedPreferences(context.getPackageName(),MODE_PRIVATE);
sValue = sharedPref.getString(sKey,defValue);
Related