简体   繁体   English

Mosby 3 MVP-在回退堆栈中的片段时始终会重新创建演示者

[英]Mosby 3 MVP - presenter is always recreated when go back to the fragment in the back stack

When we do popBackStack and return to the previous fragment in the back stack, the bundle in the method onViewCreated(View view, Bundle bundle) is always null due to the fact that onSaveInstanceState(Bundle outState) wasn't called before. 当我们执行popBackStack并返回后退堆栈中的前一个片段时,由于之前未调用过onSaveInstanceState(Bundle outState)的事实,因此onViewCreated(View view, Bundle bundle)方法中的onViewCreated(View view, Bundle bundle)始终为null。 So, when we go back, bundle is null and a presenter (and view state) is created again. 因此,当我们返回时,bundle为空,并且再次创建了演示者(和查看状态)。 How in this case we can reuse presenter and view state (and not recreate it)? 在这种情况下,我们如何重用演示者并查看状态(而不重新创建状态)?

You can see a dummy example below. 您可以在下面看到一个虚拟示例。 There is a fragment with 2 buttons. 有一个带有2个按钮的片段。 One button opens a new Fragment and another button goes to the previous one. 一个按钮打开一个新的片段,另一个按钮转到上一个片段。 When you go back, presenter and view states are recreated. 当您返回时,将重新创建演示者和视图状态。 That's not I need, but I described above why it's happenning according to the code in the library. 这不是我所需要的,但是我在上面根据库中的代码描述了为什么会发生这种情况。 Is there a way to ensure that we reuse presenter and view state when we go back? 有没有一种方法可以确保我们返回时重用演示者并查看状态?

public class FirstFragment extends MvpViewStateFragment<FirstFragmentView, FirstFragmentPresenter, FirstFragmentViewState> {

public static final String TAG = "TAG";

private Button moveToAnotherFragmentButton;
private Button moveBackButton;

@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
    final View rootFragmentView = inflater.inflate(R.layout.first_fragment, container, false);
    moveToAnotherFragmentButton = (Button) rootFragmentView.findViewById(R.id.first_fragment_go_to_another_fragment_button);
    moveBackButton = (Button) rootFragmentView.findViewById(R.id.first_fragment_back_button);
    return rootFragmentView;
}

@Override
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    moveToAnotherFragmentButton.setOnClickListener(ignored -> addToStack(FirstFragment.class));
    moveBackButton.setOnClickListener(ignored -> getFragmentManager().popBackStack());
}

@Override
@NonNull
public FirstFragmentPresenter createPresenter() {
    Log.e(TAG, "createPresenter");
    return new FirstFragmentPresenter();
}

@NonNull
@Override
public FirstFragmentViewState createViewState() {
    Log.e(TAG, "createViewState");
    return new FirstFragmentViewState();
}

@Override
public void onNewViewStateInstance() {
    Log.e(TAG, "onNewViewStateInstance");
}

private void addToStack(@NonNull final Class<? extends Fragment> fragmentClass) {
    final FragmentManager fragmentManager = getFragmentManager();
    if (fragmentManager.isDestroyed()) {
        throw new UnexpectedException("FragmentManager is destroyed");
    }

    final Fragment fragment = Fragment.instantiate(getContext(), fragmentClass.getName());

    final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

    fragmentTransaction.replace(R.id.activity_main_content_container, fragment, null)
            .addToBackStack(null)
            .commit();
}
}

The creator of this library answered this question here . 这个库的创造者回答了这个问题在这里 The short answer: "this behavior was not intended". 简短的答案:“此行为不是故意的”。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM