Android 如何使用Intent和OnActivityResult传递内容?
我的问题是: 我有两门课:Android 如何使用Intent和OnActivityResult传递内容?,android,android-intent,onactivityresult,Android,Android Intent,Onactivityresult,我的问题是: 我有两门课: FirstActivity.java SecondActivity.java 当我点击FirstActivity上的fab按钮时,我想将字符串变量传递到SecondActivity并转到SecondActivity。在SecondActivity中,我将收到该字符串并将其祝酒 我该怎么做非常感谢您将您的此代码放入晶圆厂按钮onClickListener Intent mIntent = new Intent(mContext, SecondActivity.cla
- FirstActivity.java
- SecondActivity.java
FirstActivity
上的fab按钮时,我想将字符串变量
传递到SecondActivity
并转到SecondActivity
。在SecondActivity
中,我将收到该字符串并将其祝酒
我该怎么做非常感谢您将您的此代码放入晶圆厂按钮onClickListener
Intent mIntent = new Intent(mContext, SecondActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mIntent.putExtra("key", "Your value");
startActivity(mIntent);
在SecondActivity.java上
String value = getIntent().getStringArrayExtra("key")
键
是特定变量的唯一名称,您可以根据需要指定
如果您有pass变量,那么简单地将您的变量
String str = "This is my meesage";
mIntent.putExtra("key", str);
在您的fab buttons onClick方法中
//creating and initializing an Intent object
Intent intent = new Intent(this, SecondActivity.class);
//attach the key value pair using putExtra to this intent
String user_name = "Jhon Doe";
intent.putExtra("USER_NAME", user_name);
//starting the activity
startActivity(intent);
在第二个活动onCreate方法中
//get the current intent
Intent intent = getIntent();
//get the attached extras from the intent
//we should use the same key as we used to attach the data.
String user_name = intent.getStringExtra("USER_NAME");
资料来源: