[英]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..
setting
值ClassName.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.使用
setter
和getter
方法创建一个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. 否则,我建议向要接收位置变量的类添加一个
public
或public 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.