I have an activity which contains BottomNavigationView
and NavHostFragment
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setSelectedItemId(R.id.navigation_profile);
NavController navController = Navigation.findNavController(this, R.id.myNavHostFragment);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
I have also added onActivityResult
method in this parent activity.
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this,"Parent onActivity",Toast.LENGTH_SHORT).show();
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
In the fragment, I have an ImageView
in which I am using Intent to pick an image from the user.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
this.startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);
And in the fragment's onActivityResult
I am doing this:
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
Toast.makeText(getActivity(),"OnActivityResult",Toast.LENGTH_SHORT).show();
// Result returned from launching the Intent to select image for profile
if (requestCode == RC_PHOTO_PICKER && resultCode == Activity.RESULT_OK) {
Toast.makeText(getActivity(),"Called",Toast.LENGTH_SHORT).show();
}
}
The issue is onActivityResult
is neither called in the parent activity and nor in the fragment.
I have tried various solutions provided in Answer1, Answer2, Answer3. But none of them are working!!