Pass ArrayList<? implements Parcelable> to Activity

前端 未结 6 1842
礼貌的吻别
礼貌的吻别 2020-11-27 04:20

I have searched a few topics but not found a solution to my problem.

public class Series implements Parcelable {
private String name;
private int numOfSeaso         


        
6条回答
  •  没有蜡笔的小新
    2020-11-27 05:03

    • The problem is in writing out to the parcel and reading in from the parcel ...

      @Override
      public void writeToParcel(Parcel dest, int flags) {
          dest.writeString(name);
          dest.writeInt(numOfSeason);
          dest.writeInt(numOfEpisode);
      }
      
      private void readFromParcel(Parcel in) {
          name = in.readString();
          numOfSeason = in.readInt();
          numOfEpisode = in.readInt();
      }
      
    • What you write out has to match what you read in...

      @Override
       protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      Intent i = new Intent(this,SecondActivity.class);
      
      ArrayList testing = new ArrayList();
      
      i.putParcelableArrayListExtra("extraextra", testing);
      startActivity(i);
      }
      
          /**********************************************/
      
      
      public class SecondActivity extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          ArrayList testing = this.getIntent().getParcelableArrayListExtra("extraextra");
       }
      }
      
    • The above code is having onCreate() from two different activities. The first one launches the second one; and it works fine I was able to pull the parcelable without issue.

提交回复
热议问题