且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将MainActivity的数据传递到Tab片段

更新时间:2023-12-03 16:18:16

您需要使用 Intent 才能将数据发送到另一个活动

you need use Intent in order to send data to another activity

@Override
  public void onClick(View v) {

    Book book = new Book(mBkTitle.getText().toString(),
            mBkAuthor.getText().toString());

    Intent intent = new Intent(MainActivity.this, BookActivity.class);
    intent.putExtra("Book", book);
    startActivity(intent);
  }

数据需要从Parcelable扩展

data need to extends from Parcelable

public class Book implements Parcelable {
  // book basics
  private String title;
  private String author;
}

使用 http://www.jsonschema2pojo.org/帮助您使类可拆分.

use http://www.jsonschema2pojo.org/ to help you make class parcelable.

查看本教程

https://code.tutsplus.com/tutorials/how-to-pass-data-between-activities-with-android-parcelable--cms-29559