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

单击注销时,我将传递要注销的登录用户的会话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类使用序列化在类之间传递数据。

其他回答

使用捆绑包@链接https://medium.com/@nikhildhyani365/从一个活动到另一个活动的传递数据-18df2a701142//从介质复制

           Intent I =  new Intent(MainActivity.this,Show_Details.class);

            Bundle b = new Bundle();


            int x = Integer.parseInt(age.getText().toString());
            int y = Integer.parseInt(className.getText().toString());

            b.putString("Name",name.getText().toString());

            b.putInt("Age",x);
            b.putInt("ClassName",y);

            I.putExtra("student",b);

            startActivity(I);

使用Intent@链接https://android.jlelse.eu/passing-data-between-activities-using-intent-in-android-85cb097f3016

在android应用程序的活动和其他组件之间传递数据的方法不止一种。一种是使用意图和可分割性,正如很多答案中已经提到的。

另一种优雅的方式是使用Eventbus库。

从排放活动:

EventBus.getDefault().postSticky("--your Object--");

在录制活动中:

EventBus.getDefault().removeStickyEvent("--Object class--")

其他需要考虑的事项:

提供了更多的自由,您可以传递复杂的对象,而无需以任何形式修改它们。不仅限于在活动之间传递数据,一旦设置了库,就可以使用它在应用程序管道中将数据从一个位置传递到另一个位置。例如,将此用于BottomSheetMenu到活动通信。稳定的库。简化了组件之间的通信解耦事件发送器和接收器在UI工件(例如活动、碎片)和后台线程中表现良好避免复杂和容易出错的依赖关系和生命周期问题速度快;专为高性能而优化很小(约60k罐)通过安装量超过1000000000的应用程序在实践中得到了验证具有高级功能,如传递线程、订户优先级等。

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

我们有两个活动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
    }
}

要在Java中执行此操作:

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

我使用公共静态字段来存储活动之间的共享数据,但为了最大限度地减少其副作用,您可以:

只创建一个或尽可能少的字段,然后重用它们,使它们成为对象类型,并在接收活动中将其转换为所需类型。每当它们中的任何一个不再有用时,在下一次分配之前,将其显式设置为空,由垃圾收集器收集。