如何从Android上获取额外的数据?

Adh*_*ham 719 android android-intent

如何将数据从一个活动(意图)发送到另一个活动?

我用这段代码发送数据:

Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);
Run Code Online (Sandbox Code Playgroud)

Mal*_*olm 1158

首先,使用该getIntent()方法获取已启动活动的意图:

Intent intent = getIntent();
Run Code Online (Sandbox Code Playgroud)

如果您的额外数据表示为字符串,则可以使用intent.getStringExtra(String name)方法.在你的情况下:

String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");
Run Code Online (Sandbox Code Playgroud)

  • @adham:如果你在onCreate中的一个活动中,你调用`getIntent().getStringExtra("id");`获取id字符串 (45认同)
  • 从哪里可以这个方法? (5认同)
  • @Eatlon如果您对特定库有疑问,您应该就此创建一个单独的问题。 (2认同)
  • @MelColm getExtra().getString 和 getStringExtra() 有什么区别? (2认同)

Nic*_*ckT 185

在接收活动中

Bundle extras = getIntent().getExtras(); 
String userName;

if (extras != null) {
    userName = extras.getString("name");
    // and get whatever type user account id is
}
Run Code Online (Sandbox Code Playgroud)

  • 为什么这比`getStringExtra更好?` (6认同)
  • 我的猜测是:如果附加内容可以是"null",则可以跳过整个附加内容.通过使用`getStringExtra`,您基本上将其更改为一系列`if(extras!= null){return extras.getString(name)}`.你调用的每个`getStringExtra`一个.此选项将检查一次"null",如果是这样,它根本不会打扰读取"Bundle".除此之外,`getStringExtra`也可能每次都在内部询问`getExtras`.所以你只需要调用函数. (6认同)

Sum*_*rma 38

//  How to send value using intent from one class to another class
//  class A(which will send data)
    Intent theIntent = new Intent(this, B.class);
    theIntent.putExtra("name", john);
    startActivity(theIntent);
//  How to get these values in another class
//  Class B
    Intent i= getIntent();
    i.getStringExtra("name");
//  if you log here i than you will get the value of i i.e. john
Run Code Online (Sandbox Code Playgroud)


iti*_*skj 29

加起来

设置数据

String value = "Hello World!";
Intent intent = new Intent(getApplicationContext(), NewActivity.class);
intent.putExtra("sample_name", value);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

获取数据

String value;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    value = bundle.getString("sample_name");
}
Run Code Online (Sandbox Code Playgroud)


r_a*_*ela 17

不要初始化另一个新的Intent来接收数据,而是这样做:

String id = getIntent().getStringExtra("id");
Run Code Online (Sandbox Code Playgroud)


小智 11

如果在FragmentActivity中使用,请尝试以下方法:

第一页扩展了FragmentActivity

Intent Tabdetail = new Intent(getApplicationContext(), ReceivePage.class);
Tabdetail.putExtra("Marker", marker.getTitle().toString());
startActivity(Tabdetail);
Run Code Online (Sandbox Code Playgroud)

在片段中,您只需getActivity()要先调用,

第二页扩展片段:

String receive = getActivity().getIntent().getExtras().getString("name");
Run Code Online (Sandbox Code Playgroud)


sta*_*bit 7

如果您尝试在片段中获取额外数据,那么您可以尝试使用:

放置数据使用:

Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER);
Run Code Online (Sandbox Code Playgroud)

获取数据使用:

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


  getArguments().getInt(ARG_SECTION_NUMBER);
  getArguments().getString(ARG_SECTION_STRING);
  getArguments().getBoolean(ARG_SECTION_BOOL);
  getArguments().getChar(ARG_SECTION_CHAR);
  getArguments().getByte(ARG_SECTION_DATA);

}
Run Code Online (Sandbox Code Playgroud)


Khe*_*raj 7

科特林

第一个活动

val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("key", "value")
startActivity(intent)
Run Code Online (Sandbox Code Playgroud)

第二项活动

val value = getIntent().getStringExtra("key")
Run Code Online (Sandbox Code Playgroud)

建议

始终将密钥放在常量文件中以获得更易于管理的方式。

companion object {
    val PUT_EXTRA_USER = "PUT_EXTRA_USER"
}
Run Code Online (Sandbox Code Playgroud)


Has*_*ter 6

您可以从 intent 获取任何类型的额外数据,无论它是对象、字符串还是任何类型的数据。

Bundle extra = getIntent().getExtras();

if (extra != null){
    String str1 = (String) extra.get("obj"); // get a object

    String str2 =  extra.getString("string"); //get a string
}
Run Code Online (Sandbox Code Playgroud)

最短溶液是:

Boolean isGranted = getIntent().getBooleanExtra("tag", false);
Run Code Online (Sandbox Code Playgroud)


小智 6

按意图放置数据:

Intent intent = new Intent(mContext, HomeWorkReportActivity.class);
intent.putExtra("subjectName", "Maths");
intent.putExtra("instituteId", 22);
mContext.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

按意图获取数据:

String subName = getIntent().getStringExtra("subjectName");
int insId = getIntent().getIntExtra("instituteId", 0);
Run Code Online (Sandbox Code Playgroud)

如果我们为意图使用整数值,我们必须在 中将第二个参数设置为 0 getIntent().getIntExtra("instituteId", 0)。否则,我们不使用 0,Android 会给我一个错误。