我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
你可以用心工作。
String sessionId = "my session id";
startActivity(new Intent(getApplicationContext(),SignOutActivity.class).putExtra("sessionId",sessionId));
其他回答
数据对象应扩展Parcelable或Serializable类。
Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
mIntent.putExtra("data", data);
startActivity(mIntent);
使用捆绑包@链接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
标准方法。
Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();
Bundle bundle = new Bundle();
bundle.putString(“stuff”, getrec);
i.putExtras(bundle);
startActivity(i);
现在,在第二个活动中,从捆绑包中检索数据:
获取捆绑包
Bundle bundle = getIntent().getExtras();
提取数据…
String stuff = bundle.getString(“stuff”);
您可以使用SharedPreferences。。。
登录中。SharedPreferences中的时间存储会话idSharedPreferences preferences=getSharedPreferences(“会话”,getApplicationContext().MODE_PRIVATE);编辑器编辑器=preferences.edit();editer.putString(“sessionId”,sessionId);editer.commit();注销。共享引用中的时间提取会话idSharedPreferences preferences=getSharedPreferences(“会话”,getApplicationContext().MODE_PRIVATE);String sessionId=首选项.getString(“sessionId”,null);
如果您没有所需的会话id,请删除sharedpreferences:
SharedPreferences settings = context.getSharedPreferences("session", Context.MODE_PRIVATE);
settings.edit().clear().commit();
这非常有用,因为一次保存值,然后检索任何活动。
通过捆绑对象从此活动传递参数启动另一个活动
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);