如何完成一个 Activity 并在另一个 activity 中指定片段?

How to finish one Activity and go to specify fragment in the another activity?

编辑:
你好。
我有 MainActivity,我在其中打开 Fragment A,其中:

fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.db_container, Fragment A);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();

此片段使用 LiveData 并显示 RecyclerView 列表。
然后在用户 select 某个类别之后,我用以下内容打开片段 B:

fragmentTransaction.hide(Fragment A);
fragmentTransaction.add(R.id.db_container, Fragment B);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();

此片段也使用 LiveData 并显示 RecyclerView 列表。
当用户 select 的单个项目并想要对其进行编辑时,他们将被移动到 EditItem Activity.
用户进行一些更改并保存后,activity 以命令结束:finish();
用户返回 片段 B 并且 RecyclerView 列表已更新。
问题在于,在更新 片段 B 的列表之前,先更新 片段 A 中的列表。 片段 A 中的确切代码:

item_viewmodel.getAllCategoryModel().observe(getViewLifecycleOwner(), new Observer<List<Items>>() {
        @Override
        public void onChanged(List<Items> myLists) {
            // Here the code is executed first before updating the list in fragment B.
        }
    });

那么问题来了
如何仅更新 片段 B 中的列表?
稍后从 片段 B 我将使用 interface 回调手动更新 片段 A 中的列表。
(可选)我想首先更新 Fragment B 中的列表,然后更新 Fragment A 中的列表。 如果可能的话,我不想删除 片段 A,只是隐藏它。

据我了解,您只想更新片段 B,然后手动更新片段 A。 我假设您在列表中使用的数据在片段 A 和 B 之间共享,这就是片段 A 更新的原因,因为 A 和 B 在相同对象上有观察者。 您可以在 A boolean shouldUpdate = true; 中使用布尔变量 当你移动到 B 时将其设为 false。 同样在 A 中的观察者内部,请检查 if(shouldUpdate) .