繁体   English   中英

Android 片段更新数据

[英]Android Fragment Update Data

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

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());
    }
}

在活动中,我有这两种方法。 show fragment 方法应显示带有给定文本的进度条片段,无论它现在是否可见。 我正在为已经可见的案例寻求最佳实践。

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

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);

    }
}

并且在活动中

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()) {
            fragmentManager.beginTransaction().replace(R.id.container, progressFragment, ProgressFragment.T
TAG).commit();
        }
        if (progressViewModel == null) {
            progressViewModel = new ViewModelProvider(this).get(ProgressViewModel.class);
        }
        progressViewModel.setText(text);
    }

所以新的 mvvm 模式非常适合这种模式。 这个问题的另一面是 onSaveInstanceState 是怎么做的?

暂无
暂无

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

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