I have two activities. And I passed the argument to the target activity by intent:
Bundle bundle = new Bundle();
bundle.putString("ImagePath", path);
Intent intent = new Intent(getActivity(), DetailActivity.class);
intent.putExtra("paths", bundle);
startActivity(intent);
The target activity DetailActivity
has a fragment, and I want to get the argument ImagePath
in it. Now I have two method:
I get the argument in the
DetailActivity
bygetIntent()
and then pass it to the fragment usingsetArgmunets()
I get the argument in the target fragment using
getActivity().getIntent()
directly.
I like the method 2 and use it now because the clean code. But the Android Studio tell me the message Method invocation 'getIntent' may produce 'java.lang.NullPointerException'
in getIntent()
.
So should I abandon the method 2?
Update: Final, I used the method 1, because of this answer :
From the Fragment documentation:
Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.