1

I am trying to pass an arraylist from one activity to another activity's fragment. I searched a lot for the solution but couldn't get a working answer.

This is where I am passing the arraylist.

    Intent intent = new Intent(SizeActivity.this,CartActivity.class);
                intent.putExtra("PrintList", photoPrintsList);
                startActivity(intent);

Accepting in the activity with the fragment.

public class ExampleActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);

    if (savedInstanceState == null) {
        Fragment newFragment = new CartActivityFragment();
        newFragment.setArguments(getIntent().getExtras());
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.commit();
    }
}

And this is the fragment. How do I pass data from one activity to another fragment.

 private ArrayList<PhotoPrints> printsArrayList = new ArrayList<>();
private String TAG = "CartActivityFragment";


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_cart, container, false);

    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

    Bundle b =  getArguments();
    if(b != null) {
        Log.d(TAG,b.size() + "");
        printsArrayList = b.getParcelableArrayList("PrintList");
        Log.d(TAG,"Got arraylist");
    } else {
        Log.d(TAG,"Null");
    }

}

EDIT:

I found that my onCreateView of the fragment is being called before the onCreate of the holding activity. This way I am not able to relay data from the activity to the fragment. Have a look at the Log.

09-21 17:21:32.539  22672-22672/in.reduxpress   D/CartActivityFragment﹕ Null checker
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivityFragment﹕ Null
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ Received Arraylist
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 0 /storage/emulated/0/WhatsApp/Media/WhatsApp Images/Sent/IMG-20150913-WA0014.jpg
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 0 4 X 6
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 0 Glossy
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 0 Economy
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 0 6
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 1 /storage/emulated/0/WhatsApp/Media/WhatsApp Images/Sent/IMG-20150913-WA0015.jpg
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 1 4 X 6
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 1 Glossy
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 1 Economy
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 1 6
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 2 /storage/emulated/0/WhatsApp/Media/WhatsApp Images/Sent/IMG-20150915-WA0003.jpg
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 2 4 X 6
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 2 Glossy
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 2 Economy
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 2 6
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ Size of bundle 1

How do I bypass this behaviour?

Edit2: I have tested extensively. I am able to receive the data in the activity successfully. I put it in a bundle and set the bundle as argument. But I am not able to receive the bundle in the fragment. It is always giving me a null value.

I have tried putting getArguments in onCreateView as well as onActivityCreated, both don't work. Please help.

1 Answers1

0

try this,

here's my code for passing arraylist :

Intent intent = getIntent();
intent.putExtra("key", selectImages);
setResult(RESULT_OK, intent);
 finish(); 

here is onActivityResult().

public void onActivityResult(int requestCode, int resultCode, Intent data){
            if (requestCode == 999) {           
                @SuppressWarnings("unchecked")
                ArrayList<String> ar1 =  data.getStringArrayListExtra("key"); 
                if(ar1.size()!= 0){
                for (int i = 0; i < ar1.size(); i++) {
                    String value = ar1.get(i);
                    Toast.makeText(getActivity(),
                            "Path of array in home Fragment: "+ar1,
                            Toast.LENGTH_LONG).show();
    }       }      }     }

here's Myparcelable class code:

import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;

public class ObjectA implements Parcelable {

    public ArrayList<String> choices;

   public ObjectA (ArrayList<String> choices) {
             this.choices = choices;
    }
  @SuppressWarnings("unchecked")
    public ObjectA (Parcel parcel) {
          parcel.readArrayList(null);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
         dest.writeStringList(choices);
    }

    // Method to recreate a ObjectA from a Parcel
    public static Creator<ObjectA> CREATOR = new Creator<ObjectA>() {

        @Override
        public ObjectA createFromParcel(Parcel source) {
            return new ObjectA(source);
        }

        @Override
        public ObjectA[] newArray(int size) {
            return new ObjectA[size];
        }
    };    
    }

That's it.

Ganpat Kaliya
  • 1,234
  • 2
  • 9
  • 16