我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
使用捆绑包@链接https://medium.com/@nikhildhyani365/从一个活动到另一个活动的传递数据-18df2a701142//从介质复制
Intent I = new Intent(MainActivity.this,Show_Details.class);
Bundle b = new Bundle();
int x = Integer.parseInt(age.getText().toString());
int y = Integer.parseInt(className.getText().toString());
b.putString("Name",name.getText().toString());
b.putInt("Age",x);
b.putInt("ClassName",y);
I.putExtra("student",b);
startActivity(I);
使用Intent@链接https://android.jlelse.eu/passing-data-between-activities-using-intent-in-android-85cb097f3016
其他回答
来自活动
int n= 10;
Intent in = new Intent(From_Activity.this,To_Activity.class);
Bundle b1 = new Bundle();
b1.putInt("integerNumber",n);
in.putExtras(b1);
startActivity(in);
目标活动
Bundle b2 = getIntent().getExtras();
int m = 0;
if(b2 != null){
m = b2.getInt("integerNumber");
}
通过捆绑对象从此活动传递参数启动另一个活动
Intent intent = new Intent(getBaseContext(), YourActivity.class);
intent.putExtra("USER_NAME", "xyz@gmail.com");
startActivity(intent);
检索其他活动(YourActivity)
String s = getIntent().getStringExtra("USER_NAME");
这适用于简单类型的数据类型。但如果您想在活动之间传递复杂的数据,则需要首先对其进行序列化。
这里有员工模型
class Employee{
private String empId;
private int age;
print Double salary;
getters...
setters...
}
您可以使用google提供的Gson-lib来序列化复杂的数据这样地
String strEmp = new Gson().toJson(emp);
Intent intent = new Intent(getBaseContext(), YourActivity.class);
intent.putExtra("EMP", strEmp);
startActivity(intent);
Bundle bundle = getIntent().getExtras();
String empStr = bundle.getString("EMP");
Gson gson = new Gson();
Type type = new TypeToken<Employee>() {
}.getType();
Employee selectedEmp = gson.fromJson(empStr, type);
活动之间的数据传递主要通过意向对象进行。
首先,必须使用Bundle类将数据附加到intent对象。然后使用startActivity()或startActivityForResult()方法调用活动。
您可以从博客文章“将数据传递给活动”中找到更多信息。
要在Java中执行此操作:
startActivity(new Intent(this, MainActivity.class).putExtra("userId", "2"));
你只需要在表达你的意图时发送额外的信息。
这样地:
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Variable name", "Value you want to pass");
startActivity(intent);
现在,在SecondActivity的OnCreate方法中,您可以像这样获取额外内容。
如果您发送的值过长:
long value = getIntent().getLongExtra("Variable name which you sent as an extra", defaultValue(you can give it anything));
如果发送的值是字符串:
String value = getIntent().getStringExtra("Variable name which you sent as an extra");
如果发送的值是布尔值:
Boolean value = getIntent().getBooleanExtra("Variable name which you sent as an extra", defaultValue);