How to pass ArrayList?












0















To avoid creating multiple activities I have one Activity2 that has this code that allows me to just pass an array of Fragments from any Activity I want.



privateArrayList<Fragment> fragArrayList;

fragArrayList = intent.getParcelableArrayListExtra("fragArrayList");

Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setFragment(fragArrayList.get(i));
}
});

private void setFragment(Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.replace(R.id.layout, fragment)
.addToBackStack(null)
.commit();
}


In Activity1 I have this code (That I know doesn't work)



Wrong 2nd argument type. Found: 'java.util.ArrayList<android.support.v4.app.Fragment>', required: 'java.util.ArrayList<? extends android.os.Parcelable>'


.



Intent intent= new Intent(Activity1.this, Activity2.class);

ArrayList<Fragment> fragArrayList = new ArrayList<>();
fragArrayList.add(new frag1());
fragArrayList.add(new frag2());
fragArrayList.add(new frag3());
fragArrayList.add(new frag4());
fragArrayList.add(new frag5());

intent.putParcelableArrayListExtra("fragArrayList"), fragArrayList);


The point is to make fragArrayList travel from any Activity to Activity2. And since the frags inside fragArrayList will be different depending on the activity they are coming from I cant just add them to the ArrayList inside Activity2.



And since there will be several activities is not efficient to create logic inside the Activity2 to handle from each Activity the user came from.



How can I pass the ArrayList from Activity1 to Activity2?










share|improve this question

























  • stackoverflow.com/questions/13601883/…

    – Vishal Sharma
    Nov 13 '18 at 18:08











  • follow this link

    – Vishal Sharma
    Nov 13 '18 at 18:09
















0















To avoid creating multiple activities I have one Activity2 that has this code that allows me to just pass an array of Fragments from any Activity I want.



privateArrayList<Fragment> fragArrayList;

fragArrayList = intent.getParcelableArrayListExtra("fragArrayList");

Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setFragment(fragArrayList.get(i));
}
});

private void setFragment(Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.replace(R.id.layout, fragment)
.addToBackStack(null)
.commit();
}


In Activity1 I have this code (That I know doesn't work)



Wrong 2nd argument type. Found: 'java.util.ArrayList<android.support.v4.app.Fragment>', required: 'java.util.ArrayList<? extends android.os.Parcelable>'


.



Intent intent= new Intent(Activity1.this, Activity2.class);

ArrayList<Fragment> fragArrayList = new ArrayList<>();
fragArrayList.add(new frag1());
fragArrayList.add(new frag2());
fragArrayList.add(new frag3());
fragArrayList.add(new frag4());
fragArrayList.add(new frag5());

intent.putParcelableArrayListExtra("fragArrayList"), fragArrayList);


The point is to make fragArrayList travel from any Activity to Activity2. And since the frags inside fragArrayList will be different depending on the activity they are coming from I cant just add them to the ArrayList inside Activity2.



And since there will be several activities is not efficient to create logic inside the Activity2 to handle from each Activity the user came from.



How can I pass the ArrayList from Activity1 to Activity2?










share|improve this question

























  • stackoverflow.com/questions/13601883/…

    – Vishal Sharma
    Nov 13 '18 at 18:08











  • follow this link

    – Vishal Sharma
    Nov 13 '18 at 18:09














0












0








0








To avoid creating multiple activities I have one Activity2 that has this code that allows me to just pass an array of Fragments from any Activity I want.



privateArrayList<Fragment> fragArrayList;

fragArrayList = intent.getParcelableArrayListExtra("fragArrayList");

Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setFragment(fragArrayList.get(i));
}
});

private void setFragment(Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.replace(R.id.layout, fragment)
.addToBackStack(null)
.commit();
}


In Activity1 I have this code (That I know doesn't work)



Wrong 2nd argument type. Found: 'java.util.ArrayList<android.support.v4.app.Fragment>', required: 'java.util.ArrayList<? extends android.os.Parcelable>'


.



Intent intent= new Intent(Activity1.this, Activity2.class);

ArrayList<Fragment> fragArrayList = new ArrayList<>();
fragArrayList.add(new frag1());
fragArrayList.add(new frag2());
fragArrayList.add(new frag3());
fragArrayList.add(new frag4());
fragArrayList.add(new frag5());

intent.putParcelableArrayListExtra("fragArrayList"), fragArrayList);


The point is to make fragArrayList travel from any Activity to Activity2. And since the frags inside fragArrayList will be different depending on the activity they are coming from I cant just add them to the ArrayList inside Activity2.



And since there will be several activities is not efficient to create logic inside the Activity2 to handle from each Activity the user came from.



How can I pass the ArrayList from Activity1 to Activity2?










share|improve this question
















To avoid creating multiple activities I have one Activity2 that has this code that allows me to just pass an array of Fragments from any Activity I want.



privateArrayList<Fragment> fragArrayList;

fragArrayList = intent.getParcelableArrayListExtra("fragArrayList");

Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setFragment(fragArrayList.get(i));
}
});

private void setFragment(Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.replace(R.id.layout, fragment)
.addToBackStack(null)
.commit();
}


In Activity1 I have this code (That I know doesn't work)



Wrong 2nd argument type. Found: 'java.util.ArrayList<android.support.v4.app.Fragment>', required: 'java.util.ArrayList<? extends android.os.Parcelable>'


.



Intent intent= new Intent(Activity1.this, Activity2.class);

ArrayList<Fragment> fragArrayList = new ArrayList<>();
fragArrayList.add(new frag1());
fragArrayList.add(new frag2());
fragArrayList.add(new frag3());
fragArrayList.add(new frag4());
fragArrayList.add(new frag5());

intent.putParcelableArrayListExtra("fragArrayList"), fragArrayList);


The point is to make fragArrayList travel from any Activity to Activity2. And since the frags inside fragArrayList will be different depending on the activity they are coming from I cant just add them to the ArrayList inside Activity2.



And since there will be several activities is not efficient to create logic inside the Activity2 to handle from each Activity the user came from.



How can I pass the ArrayList from Activity1 to Activity2?







java android android-fragments arraylist






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 18:19







newbieCoder.pkg

















asked Nov 13 '18 at 18:02









newbieCoder.pkgnewbieCoder.pkg

15013




15013













  • stackoverflow.com/questions/13601883/…

    – Vishal Sharma
    Nov 13 '18 at 18:08











  • follow this link

    – Vishal Sharma
    Nov 13 '18 at 18:09



















  • stackoverflow.com/questions/13601883/…

    – Vishal Sharma
    Nov 13 '18 at 18:08











  • follow this link

    – Vishal Sharma
    Nov 13 '18 at 18:09

















stackoverflow.com/questions/13601883/…

– Vishal Sharma
Nov 13 '18 at 18:08





stackoverflow.com/questions/13601883/…

– Vishal Sharma
Nov 13 '18 at 18:08













follow this link

– Vishal Sharma
Nov 13 '18 at 18:09





follow this link

– Vishal Sharma
Nov 13 '18 at 18:09












3 Answers
3






active

oldest

votes


















2














Fragments are not Parcelable, so you won't be able to pass them via intent. You could pass the fully qualified fragment class name via intent and then instantiate the fragment(s) in Activity2 via reflection.






share|improve this answer



















  • 1





    You can also build an ArrayList of Class objects, which are serializable, then use putExtra(name, arrayList), recover them with getSerializable(name), and then build the fragments with reflection directly from the class object with newInstance().

    – gicci
    Nov 13 '18 at 21:53











  • Thank you for your answers I did it!

    – newbieCoder.pkg
    Nov 14 '18 at 13:28



















0














That's a bad design passing list of fragment objects and you could only pass array of Parcelable class of object.






share|improve this answer































    0














    Success Thank you to: Greg Moens & gicci for the insight



    Activity1



    Intent intent= new Intent(Activity1.this, Activity2.class);

    ArrayList<String> fragArrayList= new ArrayList<>();

    fragArrayList.add(String.valueOf(frag1.class.getName()));
    fragArrayList.add(String.valueOf(frag2.class.getName()));
    fragArrayList.add(String.valueOf(frag3.class.getName()));
    fragArrayList.add(String.valueOf(frag4.class.getName()));
    fragArrayList.add(String.valueOf(frag5.class.getName()));

    intent.putExtra("fragArrayList"), fragArrayList);


    Activity2



    ArrayList<String> fragArrayList = new ArrayList<>();

    fragArrayList = intent.getStringArrayListExtra("fragArrayList");

    Button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    setFragment(fragArrayList.get(i));
    }
    });

    private void setFragment(String fragClass) {

    Fragment fragment = Fragment.instantiate(this, fragClass);

    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
    .replace(R.id.layout, fragment)
    .addToBackStack(null)
    .commit();
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286995%2fhow-to-pass-arraylistfragment%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Fragments are not Parcelable, so you won't be able to pass them via intent. You could pass the fully qualified fragment class name via intent and then instantiate the fragment(s) in Activity2 via reflection.






      share|improve this answer



















      • 1





        You can also build an ArrayList of Class objects, which are serializable, then use putExtra(name, arrayList), recover them with getSerializable(name), and then build the fragments with reflection directly from the class object with newInstance().

        – gicci
        Nov 13 '18 at 21:53











      • Thank you for your answers I did it!

        – newbieCoder.pkg
        Nov 14 '18 at 13:28
















      2














      Fragments are not Parcelable, so you won't be able to pass them via intent. You could pass the fully qualified fragment class name via intent and then instantiate the fragment(s) in Activity2 via reflection.






      share|improve this answer



















      • 1





        You can also build an ArrayList of Class objects, which are serializable, then use putExtra(name, arrayList), recover them with getSerializable(name), and then build the fragments with reflection directly from the class object with newInstance().

        – gicci
        Nov 13 '18 at 21:53











      • Thank you for your answers I did it!

        – newbieCoder.pkg
        Nov 14 '18 at 13:28














      2












      2








      2







      Fragments are not Parcelable, so you won't be able to pass them via intent. You could pass the fully qualified fragment class name via intent and then instantiate the fragment(s) in Activity2 via reflection.






      share|improve this answer













      Fragments are not Parcelable, so you won't be able to pass them via intent. You could pass the fully qualified fragment class name via intent and then instantiate the fragment(s) in Activity2 via reflection.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 13 '18 at 18:07









      Greg MoensGreg Moens

      51718




      51718








      • 1





        You can also build an ArrayList of Class objects, which are serializable, then use putExtra(name, arrayList), recover them with getSerializable(name), and then build the fragments with reflection directly from the class object with newInstance().

        – gicci
        Nov 13 '18 at 21:53











      • Thank you for your answers I did it!

        – newbieCoder.pkg
        Nov 14 '18 at 13:28














      • 1





        You can also build an ArrayList of Class objects, which are serializable, then use putExtra(name, arrayList), recover them with getSerializable(name), and then build the fragments with reflection directly from the class object with newInstance().

        – gicci
        Nov 13 '18 at 21:53











      • Thank you for your answers I did it!

        – newbieCoder.pkg
        Nov 14 '18 at 13:28








      1




      1





      You can also build an ArrayList of Class objects, which are serializable, then use putExtra(name, arrayList), recover them with getSerializable(name), and then build the fragments with reflection directly from the class object with newInstance().

      – gicci
      Nov 13 '18 at 21:53





      You can also build an ArrayList of Class objects, which are serializable, then use putExtra(name, arrayList), recover them with getSerializable(name), and then build the fragments with reflection directly from the class object with newInstance().

      – gicci
      Nov 13 '18 at 21:53













      Thank you for your answers I did it!

      – newbieCoder.pkg
      Nov 14 '18 at 13:28





      Thank you for your answers I did it!

      – newbieCoder.pkg
      Nov 14 '18 at 13:28













      0














      That's a bad design passing list of fragment objects and you could only pass array of Parcelable class of object.






      share|improve this answer




























        0














        That's a bad design passing list of fragment objects and you could only pass array of Parcelable class of object.






        share|improve this answer


























          0












          0








          0







          That's a bad design passing list of fragment objects and you could only pass array of Parcelable class of object.






          share|improve this answer













          That's a bad design passing list of fragment objects and you could only pass array of Parcelable class of object.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 18:12









          Ramesh YankatiRamesh Yankati

          65858




          65858























              0














              Success Thank you to: Greg Moens & gicci for the insight



              Activity1



              Intent intent= new Intent(Activity1.this, Activity2.class);

              ArrayList<String> fragArrayList= new ArrayList<>();

              fragArrayList.add(String.valueOf(frag1.class.getName()));
              fragArrayList.add(String.valueOf(frag2.class.getName()));
              fragArrayList.add(String.valueOf(frag3.class.getName()));
              fragArrayList.add(String.valueOf(frag4.class.getName()));
              fragArrayList.add(String.valueOf(frag5.class.getName()));

              intent.putExtra("fragArrayList"), fragArrayList);


              Activity2



              ArrayList<String> fragArrayList = new ArrayList<>();

              fragArrayList = intent.getStringArrayListExtra("fragArrayList");

              Button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
              setFragment(fragArrayList.get(i));
              }
              });

              private void setFragment(String fragClass) {

              Fragment fragment = Fragment.instantiate(this, fragClass);

              FragmentManager manager = getSupportFragmentManager();
              manager.beginTransaction()
              .replace(R.id.layout, fragment)
              .addToBackStack(null)
              .commit();
              }





              share|improve this answer




























                0














                Success Thank you to: Greg Moens & gicci for the insight



                Activity1



                Intent intent= new Intent(Activity1.this, Activity2.class);

                ArrayList<String> fragArrayList= new ArrayList<>();

                fragArrayList.add(String.valueOf(frag1.class.getName()));
                fragArrayList.add(String.valueOf(frag2.class.getName()));
                fragArrayList.add(String.valueOf(frag3.class.getName()));
                fragArrayList.add(String.valueOf(frag4.class.getName()));
                fragArrayList.add(String.valueOf(frag5.class.getName()));

                intent.putExtra("fragArrayList"), fragArrayList);


                Activity2



                ArrayList<String> fragArrayList = new ArrayList<>();

                fragArrayList = intent.getStringArrayListExtra("fragArrayList");

                Button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                setFragment(fragArrayList.get(i));
                }
                });

                private void setFragment(String fragClass) {

                Fragment fragment = Fragment.instantiate(this, fragClass);

                FragmentManager manager = getSupportFragmentManager();
                manager.beginTransaction()
                .replace(R.id.layout, fragment)
                .addToBackStack(null)
                .commit();
                }





                share|improve this answer


























                  0












                  0








                  0







                  Success Thank you to: Greg Moens & gicci for the insight



                  Activity1



                  Intent intent= new Intent(Activity1.this, Activity2.class);

                  ArrayList<String> fragArrayList= new ArrayList<>();

                  fragArrayList.add(String.valueOf(frag1.class.getName()));
                  fragArrayList.add(String.valueOf(frag2.class.getName()));
                  fragArrayList.add(String.valueOf(frag3.class.getName()));
                  fragArrayList.add(String.valueOf(frag4.class.getName()));
                  fragArrayList.add(String.valueOf(frag5.class.getName()));

                  intent.putExtra("fragArrayList"), fragArrayList);


                  Activity2



                  ArrayList<String> fragArrayList = new ArrayList<>();

                  fragArrayList = intent.getStringArrayListExtra("fragArrayList");

                  Button.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                  setFragment(fragArrayList.get(i));
                  }
                  });

                  private void setFragment(String fragClass) {

                  Fragment fragment = Fragment.instantiate(this, fragClass);

                  FragmentManager manager = getSupportFragmentManager();
                  manager.beginTransaction()
                  .replace(R.id.layout, fragment)
                  .addToBackStack(null)
                  .commit();
                  }





                  share|improve this answer













                  Success Thank you to: Greg Moens & gicci for the insight



                  Activity1



                  Intent intent= new Intent(Activity1.this, Activity2.class);

                  ArrayList<String> fragArrayList= new ArrayList<>();

                  fragArrayList.add(String.valueOf(frag1.class.getName()));
                  fragArrayList.add(String.valueOf(frag2.class.getName()));
                  fragArrayList.add(String.valueOf(frag3.class.getName()));
                  fragArrayList.add(String.valueOf(frag4.class.getName()));
                  fragArrayList.add(String.valueOf(frag5.class.getName()));

                  intent.putExtra("fragArrayList"), fragArrayList);


                  Activity2



                  ArrayList<String> fragArrayList = new ArrayList<>();

                  fragArrayList = intent.getStringArrayListExtra("fragArrayList");

                  Button.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                  setFragment(fragArrayList.get(i));
                  }
                  });

                  private void setFragment(String fragClass) {

                  Fragment fragment = Fragment.instantiate(this, fragClass);

                  FragmentManager manager = getSupportFragmentManager();
                  manager.beginTransaction()
                  .replace(R.id.layout, fragment)
                  .addToBackStack(null)
                  .commit();
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 13:27









                  newbieCoder.pkgnewbieCoder.pkg

                  15013




                  15013






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286995%2fhow-to-pass-arraylistfragment%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues