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

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

本案的任何替代方案


当前回答

来自活动

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

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

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

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

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

从排放活动:

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

在录制活动中:

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

其他需要考虑的事项:

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

我在类中使用静态字段,并获取/设置它们:

喜欢:

public class Info
{
    public static int ID      = 0;
    public static String NAME = "TEST";
}

要获取值,请在“活动”中使用:

Info.ID
Info.NAME

要设置值:

Info.ID = 5;
Info.NAME = "USER!";

您可以尝试共享首选项,这可能是在活动之间共享数据的一个很好的选择

保存会话id-

SharedPreferences pref = myContexy.getSharedPreferences("Session 
Data",MODE_PRIVATE);
SharedPreferences.Editor edit = pref.edit();
edit.putInt("Session ID", session_id);
edit.commit();

为了得到他们-

SharedPreferences pref = myContexy.getSharedPreferences("Session Data", MODE_PRIVATE);
session_id = pref.getInt("Session ID", 0);