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

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

本案的任何替代方案


当前回答

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

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

其他回答

最简单的方法是将会话id传递给用于启动活动的Intent中的注销活动:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);

访问下一个活动的意图:

String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");

Intents的文档有更多信息(请参阅标题为“附加”的部分)。

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

这非常有用,因为一次保存值,然后检索任何活动。

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

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

您可以通过意图在两个活动之间进行沟通。每当您通过登录活动导航到任何其他活动时,都可以将sessionId设置为intent,并通过getIntent()在其他活动中获取。以下是代码段:

登录活动:

Intent intent = new Intent(YourLoginActivity.this,OtherActivity.class);
intent.putExtra("SESSION_ID",sessionId);
startActivity(intent);
finishAfterTransition();

其他活动:

在onCreate()中或需要它调用的任何位置getIntent().getStringExtra(“SESSION_ID”);此外,确保检查intent是否为空,并且在两个活动中传递的intent密钥应该相同。以下是完整的代码片段:

if(getIntent!=null && getIntent.getStringExtra("SESSION_ID")!=null){
  sessionId = getIntent.getStringExtra("SESSION_ID");
}

但是,我建议您使用AppSharedPreferences存储sessionId,并在需要时从中获取。

数据对象应扩展Parcelable或Serializable类。

Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
mIntent.putExtra("data", data);
startActivity(mIntent);