我有这样一个情况,通过登录页面登录后,每个活动页面上都会有一个“退出”按钮
。
点击“退出”后,我将传递已登录用户的会话ID
以进行注销。 有人能指导我如何使会话ID
在所有活动
中可用吗?
这种情况是否有任何替代方案?
我有这样一个情况,通过登录页面登录后,每个活动页面上都会有一个“退出”按钮
。
点击“退出”后,我将传递已登录用户的会话ID
以进行注销。 有人能指导我如何使会话ID
在所有活动
中可用吗?
这种情况是否有任何替代方案?
Intent
。Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
mIntent.putExtra("data", data);
startActivity(mIntent);
public class DataHolder {
private static DataHolder dataHolder;
private List<Model> dataList;
public void setDataList(List<Model>dataList) {
this.dataList = dataList;
}
public List<Model> getDataList() {
return dataList;
}
public synchronized static DataHolder getInstance() {
if (dataHolder == null) {
dataHolder = new DataHolder();
}
return dataHolder;
}
}
从您的FirstActivity开始
private List<Model> dataList = new ArrayList<>();
DataHolder.getInstance().setDataList(dataList);
在SecondActivity中
private List<Model> dataList = DataHolder.getInstance().getDataList();
SharedPreferences pref = myContexy.getSharedPreferences("Session
Data",MODE_PRIVATE);
SharedPreferences.Editor edit = pref.edit();
edit.putInt("Session ID", session_id);
edit.commit();
SharedPreferences pref = myContexy.getSharedPreferences("Session Data", MODE_PRIVATE);
session_id = pref.getInt("Session ID", 0);
请在 CurrentActivity.java 中编写以下代码:
Intent i = new Intent(CurrentActivity.this, SignOutActivity.class);
i.putExtra("SESSION_ID",sessionId);
startActivity(i);
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_out);
Intent intent = getIntent();
// check intent is null or not
if(intent != null){
String sessionId = intent.getStringExtra("SESSION_ID");
Log.d("Session_id : " + sessionId);
}
else{
Toast.makeText(SignOutActivity.this, "Intent is null", Toast.LENGTH_SHORT).show();
}
}
Ctrl+F
在页面上搜索“putex”时,这是我找到的第一个结果。看到这样的代码后,我感到放心;它也没有冗余。 - The Amateur Coder从当前活动启动另一个活动并通过Bundle对象传递参数
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...
}
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);
第一种方法:在当前 Activity 中,当您创建意图对象以打开新屏幕时:
String value="xyz";
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("key", value);
startActivity(intent);
然后在下一个Activity的onCreate方法中,检索从上一个Activity传递过来的值:
if (getIntent().getExtras() != null) {
String value = getIntent().getStringExtra("key");
//The key argument must always match that used send and retrieve value from one activity to another.
}
第二个方法:您可以创建一个Bundle对象,将值放入Bundle中,然后从当前活动将Bundle对象放入Intent中 -
String value="xyz";
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", value);
intent.putExtra("bundle_key", bundle);
startActivity(intent);
然后在下一个Activity的onCreate方法中,获取从前一个Activity传递过来的值:
if (getIntent().getExtras() != null) {
Bundle bundle = getIntent().getStringExtra("bundle_key");
String value = bundle.getString("key");
//The key argument must always match that used send and retrieve value from one activity to another.
}
您还可以使用 bean 类通过序列化在类之间传递数据。
第一个活动:
Intent intent = new Intent(getApplicationContext(), ClassName.class);
intent.putExtra("Variable name", "Value you want to pass");
startActivity(intent);
第二个活动:
String str= getIntent().getStringExtra("Variable name which you sent as an extra");
/*
* If you are from transferring data from one class that doesn't
* extend Activity, then you need to do something like this.
*/
public class abc {
Context context;
public abc(Context context) {
this.context = context;
}
public void something() {
context.startactivity(new Intent(context, anyone.class).putextra("key", value));
}
}
SharedPreferences
是您可以执行此操作的一种方式。
VaporSharedPreferences
实现为 Singleton,因此这是一种选择,在 Vapor API 中它有一个重载的 .put(...)
方法,因此您不必明确担心您正在提交的数据类型 - 只要它受支持即可。 它也是流畅的,因此您可以链接调用:$.prefs(...).put("val1", 123).put("val2", "Hello World!").put("something", 3.34);
Intent
。在 Vapor API 中,您还可以在 VaporIntent
上使用可链接的重载 .put(...)
方法。$.Intent().put("data", "myData").put("more", 568)...
正如其他答案中提到的那样,您可以将其作为额外参数传递。您可以从您的Activity
中检索额外参数,此外,如果您正在使用VaporActivity
,则这将自动完成,因此您可以使用以下代码:
this.extras()
Activity
中。使用全局类:
public class GlobalClass extends Application
{
private float vitamin_a;
public float getVitaminA() {
return vitamin_a;
}
public void setVitaminA(float vitamin_a) {
this.vitamin_a = vitamin_a;
}
}
您可以从所有其他类中调用此类的setter和getter。为了做到这一点,您需要在每个Activity中创建一个GlobalClass-Object:
GlobalClass gc = (GlobalClass) getApplication();
然后你可以调用例如:
gc.getVitaminA()