我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。

单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?

本案的任何替代方案


当前回答

要在Java中执行此操作:

startActivity(new Intent(this, MainActivity.class).putExtra("userId", "2"));

其他回答

更新注意,我已经提到了SharedPreference的使用。它有一个简单的API,可以跨应用程序的活动访问。但这是一个笨拙的解决方案,如果您传递敏感数据,则会带来安全风险。最好使用意图。它包含大量重载方法,可用于在活动之间更好地传输许多不同的数据类型。看看intent.putExtra。这个链接很好地展示了putExtra的用法。

在活动之间传递数据时,我的首选方法是为相关活动创建一个静态方法,其中包含启动意图所需的参数。这样就可以方便地设置和检索参数。所以它看起来像这样

public class MyActivity extends Activity {
    public static final String ARG_PARAM1 = "arg_param1";
...
public static getIntent(Activity from, String param1, Long param2...) {
    Intent intent = new Intent(from, MyActivity.class);
        intent.putExtra(ARG_PARAM1, param1);
        intent.putExtra(ARG_PARAM2, param2);
        return intent;
}

....
// Use it like this.
startActivity(MyActvitiy.getIntent(FromActivity.this, varA, varB, ...));
...

然后,您可以为预期活动创建意图,并确保您拥有所有参数。你可以将碎片调整为。上面的一个简单示例,但你明白了。

第一种方式:在当前“活动”中,当您创建一个意图打开新屏幕的对象时:

String value="xyz";
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);    
intent.putExtra("key", value);
startActivity(intent);

然后在onCreate方法的nextActivity中,检索从上一个活动传递的值:

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对象放入当前活动的意图中-

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);

然后在onCreate方法的nextActivity中,检索从上一个活动传递的值:

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类使用序列化在类之间传递数据。

来自活动

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");
}

//您的问题是,您希望在登录后存储会话id,并将该会话id用于要注销的每个活动。

//问题的解决方案是成功登录后必须将会话id存储在公共变量中。每当您需要注销会话id时,您可以访问该变量并将变量值替换为零。

//Serializable class

public class YourClass  implements Serializable {
     public long session_id = 0;
}

您可以使用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;
 }
}

从您的第一次活动

private List<Model> dataList = new ArrayList<>();
DataHolder.getInstance().setDataList(dataList);

关于第二项活动

private List<Model> dataList = DataHolder.getInstance().getDataList();