1

I have a class WeatherFragment that extends Fragment class. I created an instance of it in the launcher activity and inflated it in a layout. Is it possible for me to to send the fragment object as an intent extra to some other activity in my project instead of creating a new instance of WeatherFragment?

Don't have a code for this. Its just an interview question.

  • [this](http://stackoverflow.com/questions/2736389/how-to-pass-object-from-one-activity-to-another-in-android) or [this](http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) could help – Keale Sep 16 '15 at 07:46
  • No not regular objects, my concern is if Intent class can pass fragment instances as serializable or parcelable objects. – Shubham Pandey Sep 16 '15 at 09:02

3 Answers3

0

I think you can, but it will not be good. A quick search brought me to this question with an answer that said:

You wouldn't. At most, you would follow @Ribose's answer -- pass a flag into the activity via an extra to indicate what set of fragments to create.

Your question is not so specific. This question is specific to what the OP wants, but maybe one of the answers could help you.


P.S. If you would like to experiment though, you can have your WeatherFragment implement Parcelable. Then pass it from one activity to another activity through intent. This answer will tell you how and you could do it like so (modified to extend Fragment class)

public class WeatherFragment extends implements Parcelable {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment, container, false);
    }

    /* everything below here is for implementing Parcelable */

    // 99.9% of the time you can just ignore this
    public int describeContents() {
        return 0;
    }

    // write your object's data to the passed-in Parcel
    public void writeToParcel(Parcel out, int flags) {
        //code
    }

    // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
        public MyParcelable createFromParcel(Parcel in) {
            return new MyParcelable(in);
        }

        public MyParcelable[] newArray(int size) {
            return new MyParcelable[size];
        }
    };

    // example constructor that takes a Parcel and gives you an object populated with it's values
    private MyParcelable(Parcel in) {
        //code
    }

    //other methods
}

Then, from the answer again, you can use it like so:

Intent intent = new Intent();
intent.putExtra(KEY_EXTRA, weatherFragment);

From the answer again (You really should read this answer), you get it like so:

Intent intent = getIntent();
WeatherFragment weatherFragment = (WeatherFragment) intent.getParcelableExtra(MainActivity.KEY_EXTRA);

I have not tested this so I'm not sure if it would work.

Community
  • 1
  • 1
Keale
  • 3,924
  • 3
  • 29
  • 46
  • Yes I know it is unethical. You can always declare separate instances for separate activities. I just wanted to know if it is possible or not, as I was asked the same as an interview question. – Shubham Pandey Sep 16 '15 at 08:57
  • Yes. There are ways you can do it, But although one or two of those ways could work, it could lead to undesired behavior in the future so it is not recommended. TLDR; *Yes you can but you should not*. – Keale Sep 16 '15 at 09:11
  • Ok, can you be more specific on undesired behaviour? – Shubham Pandey Sep 16 '15 at 09:15
0

Possible but I won't recommend it. But you can get the fragment object by using findFragmentById or findFragmentByTag to get the object.

semirturgay
  • 4,151
  • 3
  • 30
  • 50
0

Between different Acitvities you can not since Fragment does not implement Serializable or Parcelable.

Sure you can make your Fragment implement those interfaces but this way you won't actually be passing Fragment, just some state of that Fragment which you then serialize yourself.

Within the same Activity you can have your fragment back when the Activity gets recreated if you use FragmentManager.putFragment() in onSaveState() and getFragment() in onCreate(). This is not needed usually.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158