Android片段更新数据

问题描述

我在尝试从活动中更新片段时遇到问题。 我有一个进度条片段。

public class ProgressFragment extends Fragment {
    public static final String TAG = "ProgressFragment";
    public static final String KEY_PROGRESS_TEXT = "keyProgresstext";
    private static final String KEY_PROGRESS = "keyProgress";
    private ProgressBar progressBar;
    private String progresstext;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_progress,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);
        progressBar = view.findViewById(R.id.progress_bar);
        TextView progressBarInfo = view.findViewById(R.id.progress_bar_info);
        Bundle args = savedInstanceState == null ? getArguments() : savedInstanceState;
        if (args == null) {
            return;
        }
        int progress = args.getInt(KEY_PROGRESS);
        progresstext = args.getString(KEY_PROGRESS_TEXT,getString(R.string.progress_info));
        progressBar.setProgress(progress);
        progressBarInfo.setText(progresstext);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_PROGRESS_TEXT,progresstext);
        outState.putInt(KEY_PROGRESS,progressBar.getProgress());
    }
}

在活动中,我有这两种方法。无论是否可见,showfragment方法都应使用给定的文本显示进度条片段。我要求在已经可见的情况下采取最佳做法。

private ProgressFragment getProgressFragment(){
        progressFragment = (ProgressFragment)fragmentManager.findFragmentByTag(ProgressFragment.TAG);
        if(progressFragment == null){
            progressFragment = new ProgressFragment();
        }
        return progressFragment;
    }

    private void showProgress(String text) {
        progressFragment = getProgressFragment();
        if(!progressFragment.isAdded()){
            Bundle args = new Bundle();
            args.putString(ProgressFragment.KEY_PROGRESS_TEXT,text);
            progressFragment.setArguments(args);
        }
        // How shall I set my text data if the progress fragment is already added
        // So it won't pass the lifecycle where I am extracting progress text from args.
        fragmentManager.beginTranslaction().replace(.....,TAG).commit();
    }

解决方法

我一直在研究最佳体验。 我已经实现了一个ProgressViewModel

<html>
    <head>
        <title>PageB</title>
    </head>
    <body>
        <div style="height:900px;"></div>
        <p id="anchor">Chapter 4</p>
        <div style="height:900px;"></div>

    </body>
</html>

然后分段

public class ProgressViewModel extends ViewModel {
    private final MutableLiveData<String> progressText = new MutableLiveData<>();

    public LiveData<String> getText() {
        return progressText;
    }

    public void setText(String text) {
        progressText.setValue(text);
    }
}

活动中

public class ProgressFragment extends Fragment {
    public static final String TAG = "ProgressFragment";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_progress,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);
        FragmentActivity activity = requireActivity();
        TextView progressBarInfo = view.findViewById(R.id.progress_bar_info);
        ProgressViewModel progressViewModel = new ViewModelProvider(activity).get(ProgressViewModel.class);
        progressViewModel.getText().observe(activity,progressBarInfo::setText);

    }
}

因此,新的mvvm模式非常适合此模式。 这个问题的另一面是onSaveInstanceState如何完成?