Android - getIntent() from a Fragment
You can use a getIntent()
with Fragments
but you need to call getActivity()
first. Something like getActivity().getIntent().getExtras().getString("image")
could work.
You can also achieve this with Fragment
using setArguments()
and getArguments()
, like the following:
MyFragment fragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("image", fileName);
fragment.setArguments(bundle);//Here pass your data
Now inside your fragment class ,for example inside onCreate()
or onCreateView()
do the following:
String fileName = this.getArguments().getString("image");
It's not that you can't pass data, it's that you don't want to.
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.
If you take a look at the Fragment
documentation, it should walk you through how to do this.