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

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

本案的任何替代方案


当前回答

在CurrentActivity.java中编写以下代码

Intent i = new Intent(CurrentActivity.this, SignOutActivity.class);
i.putExtra("SESSION_ID",sessionId);
startActivity(i);

按以下方式访问SignOutActivity.java中的SessionId

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

其他回答

第一项活动:

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

补充答案:密钥串的命名惯例

传递数据的实际过程已经得到了回答,但大多数回答都使用硬编码字符串作为Intent中的密钥名称。仅在应用程序中使用时,这通常很好。但是,文档建议对标准化数据类型使用EXTRA_*常量。

示例1:使用Intent.EXTRA_*键

第一项活动

Intent intent = new Intent(getActivity(), SecondActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, "my text");
startActivity(intent);

第二项活动:

Intent intent = getIntent();
String myText = intent.getExtras().getString(Intent.EXTRA_TEXT);

示例2:定义自己的静态final键

如果Intent.EXTRA_*字符串之一不符合您的需要,您可以在第一个活动开始时定义自己的字符串。

static final String EXTRA_STUFF = "com.myPackageName.EXTRA_STUFF";

如果您只在自己的应用程序中使用密钥,那么包含程序包名称只是一种惯例。但如果您正在创建其他应用程序可以使用Intent调用的某种服务,则必须避免命名冲突。

第一项活动:

Intent intent = new Intent(getActivity(), SecondActivity.class);
intent.putExtra(EXTRA_STUFF, "my text");
startActivity(intent);

第二项活动:

Intent intent = getIntent();
String myText = intent.getExtras().getString(FirstActivity.EXTRA_STUFF);

示例3:使用字符串资源键

虽然文档中没有提到,但这个答案建议使用String资源来避免活动之间的依赖关系。

字符串.xml

 <string name="EXTRA_STUFF">com.myPackageName.MY_NAME</string>

第一项活动

Intent intent = new Intent(getActivity(), SecondActivity.class);
intent.putExtra(getString(R.string.EXTRA_STUFF), "my text");
startActivity(intent);

第二项活动

Intent intent = getIntent();
String myText = intent.getExtras().getString(getString(R.string.EXTRA_STUFF));

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

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

您还可以通过创建可分割类来传递自定义类对象。使其可分割的最佳方法是编写类,然后简单地将其粘贴到如下站点http://www.parcelabler.com/.单击构建,您将获得新代码。复制所有这些并替换原始的类内容。然后-

Intent intent = new Intent(getBaseContext(), NextActivity.class);
Foo foo = new Foo();
intent.putExtra("foo", foo);
startActivity(intent);

并在NextActivity中获得结果-

Foo foo = getIntent().getExtras().getParcelable("foo");

现在,您可以像使用一样简单地使用foo对象。

在活动之间传递数据最方便的方法是传递意图。在要发送数据的第一个活动中,应添加代码,

String str = "My Data"; //Data you want to send
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("name",str); //Here you will add the data into intent to pass bw activites
v.getContext().startActivity(intent);

您还应导入

import android.content.Intent;

然后在下一个活动(SecondActivity)中,您应该使用以下代码从意图中检索数据。

String name = this.getIntent().getStringExtra("name");