简体   繁体   English

如何使用putExtra将数据发送到多个活动

[英]how to send data to multiple activities using putExtra

I have the following code: 我有以下代码:

Intent intent = new Intent();
                intent.setClass(getApplicationContext(), DetailView.class);
                intent.putExtra("position", Integer.toString(position + 1)); 
                startActivity(intent);

Here as you can see I am sending the value of position to a class named DetailView using putExtra. 正如您所看到的,我正在使用putExtra将position的值发送到名为DetailView的类。 I want to send the same position to another class, I don't want to launch the activity, I want to just send the value of "position". 我想将相同的职位发送给另一个类,我不想启动活动,我只想发送“职位”的值。 Can any one of you please let me know how can that be done. 可以请你们让我知道该怎么做。 All suggestions are welcome. 欢迎所有建议。 Thanks in advance. 提前致谢。 Please let me know if more information is required. 如果需要更多信息,请告诉我。

If you don't want to launch other activity there are three options that i can provide you. 如果您不想启动其他活动,我可以为您提供三种选择。

1. Create public static int position and access it view class name. 1.创建public static int position并访问它的视图class名称。

When setting value ClassName.position=value here.. ClassName.position=value here.. settingClassName.position=value here..

When gettting from any class used as ClassName.position; gettting从任何class用作ClassName.position;

2. Create a class with setter and getter method. 2.使用settergetter方法创建一个class

3. Store value in sharedPrefrence 3.将值存储在sharedPrefrence

Update each above when your position value change. 当您的position值更改时,请更新以上各项。

Where else will this class that you need to pass the position variable be used? 您还需要在哪里通过传递位置变量的此类?

If you are already creating a new instance of the class the best way might be to simply pass the value through a constructor. 如果您已经在创建类的新实例,则最好的方法可能是简单地通过构造函数传递值。

Otherwise I would recommend adding either a public or public static method to the class that you want to receive the position variable. 否则,我建议向要接收位置变量的类添加一个publicpublic static方法。

public class PositionReceiver {
    private int mPosition; 

    public void setPosition(int position){
        mPosition = position;
    }

}

or 要么

public class PositionReceiver {
    private static int sPosition; 

    public static setPosition(int position){
        sPosition = position;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Adapter类的putExtra将数据发送到新的Activity - How to send data using putExtra of the an Adapter Class to a new Activity 如何使用 putExtra 从 Activity 向 Java Class 发送数据? - How to send data by using putExtra from an Activity to a Java Class? Android:在活动之间传递数据-多个intent.putExtra无法正常工作? - Android: Passing data between activities - multiple intent.putExtra not working? putExtra数据不在活动之间传递 - putExtra data not passing between activities 如何使用 ViewModel 在活动之间发送数据 - How to send data between activities using ViewModel 使用putExtra和getExtra发送数据 - Send data with putExtra and getExtra 如何使用 intent 将 bitmap 数据发送到另一个屏幕并使用 .putExtra() 将其加载到 kotlin 中的图像视图中? - How do I send bitmap data using intent to another screen and load it into a Image View in kotlin using .putExtra()? 如何在Android中以受控方式将数据发送到多个活动? - how to send data to multiple activities in controlled manner in Android? 如何使用putExtra打开另一个应用程序并为其发送一些数据? - How to use putExtra to open another app and send some data for it? 如何使用putExtra发送第二张图片而不替换第一张图片 - How to send second image using putExtra without replacing the first one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM