Android – getIntent() from a Fragment-ThrowExceptions

Exception or error:

I am trying to pass a bitmap from one fragment to another–and am using this post as a guide:

send Bitmap using intent Android

What i am having trouble with is in the receiving activity fragment using getIntent(). It doesn’t recognize the method. there are some posts out there saying that its not possible to use getIntent() in a fragment… but there must be a way? should the code go in the host activity?

here is what i am trying:

public void onCreate(Bundle savedInstanceState) {

    String filename = getIntent().getStringExtra("image");
    try {
        FileInputStream is = this.openFileInput(filename);
        imageBitmap = BitmapFactory.decodeStream(is);
    } catch (Exception e) {
How to solve:

You can use a getIntent() with Fragments but you need to call getActivity() first. Something like getActivity().getIntent().getExtras().getString("image") could work.


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.


If you want get intent data, you have to call Fragment's method getArguments(), which returns Bundle with extras.


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");

Hope this also helps

Leave a Reply

Your email address will not be published. Required fields are marked *