我正在一个包含多个片段的活动项目中工作。现在我需要在这些片段之间共享一些数据(整数、字符串、arraylist)。
第一次我使用了静态字段,但我认为这是一种不好的方式,然后我找到这个解决方案。
但在我的情况下,没有按钮可以点击,我只需要在片段之间导航,有没有简单的方法在片段之间共享数据?
第一次我使用了静态字段,但我认为这是一种不好的方式,然后我找到这个解决方案。
但在我的情况下,没有按钮可以点击,我只需要在片段之间导航,有没有简单的方法在片段之间共享数据?
我认为对于你来说最好的解决方案是将变量放在主活动中,并从片段中访问它们。我的意思是,如果你必须在所有片段中执行相同的操作,那么可以在活动中编写代码,只需调用所需的方法即可。
你需要使用接口来实现这个目的。
public interface DataCommunication {
public String getMyVariableX();
public void setMyVariableX(String x);
public int getMyVariableY();
public void setMyVariableY(int y);
}
public class MainActivity extends Activity implements DataCommunication {
private String x;
private int y;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
}
...
@Override
public String getMyVariableX(){
return x;
}
@Override
public void setMyVariableX(String x){
//do whatever or just set
this.x = x;
}
@Override
public int getMyVariableY(){
return y;
}
@Override
public void setMyVariableY(int y){
//do whatever or just set
this.y = y;
}
...
然后,在所有的片段中添加该活动:
public class Fragment_1 extends Fragment{
DataCommunication mCallback;
@Override
public void onAttach(Context context) {
super.onAttach(context);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (DataCommunication) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement DataCommunication");
}
}
...
Bundle
。您可以按照以下步骤操作:
Fragment A:
Bundle b = new Bundle();
b.putString("Key", "YourValue");
b.putInt("YourKey", 1);
FragmentB fragB = new FragmentB();
fragB.setArguments(b);
getFragmentManager().beginTransaction().replace(R.id.your_container, fragB);
片段 B:
Bundle b = this.getArguments();
if(b != null){
int i = b.getInt("YourKey");
String s =b.getString("Key");
}
我发现这是从一个片段向另一个片段发送数据的最简单方法。希望能对某些人有所帮助。
SharedPreferences settings = context.getSharedPreferences("Preferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("string1", var);
editor.putBoolean("bool1", var);
...
editor.commit();
选项2:如果您已经实例化了一个片段,您可以创建一个设置方法来在片段中存储某种信息。例如,如果您想要传递一个字符串数组,您可以在片段中创建一个数组变量(带有setter方法)。稍后,当您已经实例化这个片段时,您可以使用setter方法将这个数组传递到片段中。
使用 Bundle
使用持久性存储
使用 Bundle:
Bundle bundle=new Bundle();
bundle.putString("firstName",Fist);
bundle.putString("LastName",lastN);
第二个持久化存储:- 您可以查看如何将数据存储在持久化存储中。 希望对您有所帮助
使用在官方ViewModel 文档 中提出的SharedViewModel。
首先实现fragment-ktx以更轻松地实例化您的viewmodel。
dependencies {
implementation "androidx.fragment:fragment-ktx:1.2.2"
}
然后,您只需要将要与其他片段共享的数据放入视图模型中即可。
class SharedViewModel : ViewModel() {
val selected = MutableLiveData<Item>()
fun select(item: Item) {
selected.value = item
}
}
class MasterFragment : Fragment() {
private val model: SharedViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
itemSelector.setOnClickListener { item ->
model.select(item)
}
}
}
然后,在你的Fragment目标处,只需监听这个值
Fragment B
class DetailFragment : Fragment() {
private val model: SharedViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
model.selected.observe(viewLifecycleOwner, Observer<Item> { item ->
// Update the UI
})
}
}
1. 使用 BroadCastReceiver(有点复杂)
2. 使用 SharePreference
3. 将数据保存到 MyApplication 扩展的 Application 中