gpt4 book ai didi

android-fragments - 删除+添加后片段未收到 LiveData 更新

转载 作者:行者123 更新时间:2023-12-01 11:15:04 26 4
gpt4 key购买 nike

我目前正在尝试了解 fragmentViewModelLiveData 相关的生命周期。

我有 2 个 fragmentsfragmentAfragmentB。我在每个 fragmentonCreate 方法中添加了 Observer

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedViewModel = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
sharedViewModel.getText().observe(this, new Observer<CharSequence>() {
@Override
public void onChanged(CharSequence charSequence) {
editText.setText(charSequence);
}
});
}

每个 fragment 都有一个按钮,可以更改共享 ViewModel 中的 LiveData

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
[...]

buttonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sharedViewModel.setText(editText.getText());
}
});

[...]
}

SharedViewModel:

public class SharedViewModel extends ViewModel {
private MutableLiveData<CharSequence> text = new MutableLiveData<>();

public void setText(CharSequence input) {
text.setValue(input);
}

public LiveData<CharSequence> getText() {
return text;
}
}

当我点击一个按钮时,我将 片段 替换为另一个按钮。

public class MainActivity extends AppCompatActivity {
Fragment fragmentA = new FragmentA();
Fragment fragmentB = new FragmentB();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container_a, fragmentA)
.commit();
}
}

public void switchToA(View v) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, fragmentA)
.commit();
}

public void switchToB(View v) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, fragmentB)
.commit();
}
}

Replace 导致 fragment 被完全销毁,并在下次添加时再次运行它的 onCreate 方法。我可以确认 onCreate 是为每个放置在屏幕上的 fragment 调用的,并且添加了 Observer。但是一旦我替换了一个 fragment 并重新添加它,它就完全停止在 onChanged 中获取任何更新。甚至是它自己发送的那些。 onChanged 不再触发。我不明白为什么。

编辑:

我实际上发现 LiveData 类中的 followig if 检查返回了 第二次 我尝试添加 Observer (替换第一个的 fragment 之后):

@MainThread
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
assertMainThread("observe");
if (owner.getLifecycle().getCurrentState() == DESTROYED) {
// ignore
return;
}

因此,不再添加Observer。为什么 getCurrentState() 在我尝试重新添加 fragment 时返回 DESTROYED

简而言之:当 fragment 被移除时 Observer 被移除,但它不会在下一个添加另一个 Observer添加片段的时间。

最佳答案

根据 Lifecycle.State.DESTROYED documentation :

After this event, this Lifecycle will not dispatch any more events.

即,DESTROYED 是一个终止状态,一旦它被销毁,该生命周期将永远被销毁。

这意味着有两种正确的方法可以做你想做的事:

  1. 每次调用 switchToAswitchToB 时创建一个新的 Fragment 实例。由于当您删除片段时所有状态都被破坏,因此您不会通过重用片段实例获得任何好处。

  2. 不要使用 replace,而是使用 attach()detach()(即附上您要显示,请分离要隐藏的那个)。片段在分离时会保持其状态(它们不会被销毁),因此重新附加它会将其移回恢复状态。

关于android-fragments - 删除+添加后片段未收到 LiveData 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53130047/

26 4 0