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

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

本案的任何替代方案


当前回答

您还可以通过创建可分割类来传递自定义类对象。使其可分割的最佳方法是编写类,然后简单地将其粘贴到如下站点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对象。

其他回答

换句话说,您可以使用接口传递数据。

我们有两个活动A,B,那么我该怎么做,创建一个界面,如:

public interface M{
    void data(String m);
}

然后,您可以调用赋值给这个方法,如下面A类中的代码所示:

public class A extends AppCompatActivity{
    
   M m;   //inteface name
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a);
       
        m= (M) getActivity();

    //now call method in interface and send data im sending direct you can use same on click

    m.data("Rajeev");
    }
}

现在您必须在类B中实现该接口:

public class B extends AppCompatActivity implements M{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b);
    }

    @Override
    public void data(String m) {
        you can use m as your data to toast the value here it will be same value what you sent from class A
    }
}

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

传递数据的实际过程已经得到了回答,但大多数回答都使用硬编码字符串作为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));

标准方法。

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

来自活动

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

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

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