在 activity 中按下返回后刷新到 activity 的片段

refresh fragment that gone to activity after back pressed in that activity

我在选项卡式 activity.It 中有一个片段从 onclicklistener 重定向到 activity.After 所做的更改 activity.User 将在我的片段中返回 button.So 我需要一个从 activity.Or 返回后触发的侦听器可能在用户进行更改后我给用户一个按钮返回到该片段并重新加载片段 meanwhile.I 不知道哪种方式。 我在每件事上都尝试了 onResume() onStart() 不起作用,因为在该事务中没有发生任何碎片。 我试过 addOnBackStackChangedListener() 但它应该放在 activity 而不是片段中。 所以我不知道该怎么办? 谢谢你的时间

public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             final Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_history, container, false);
        getFragmentManager().addOnBackStackChangedListener(
                new FragmentManager.OnBackStackChangedListener() {
                    @Override
                    public void onBackStackChanged() {
                        getFragmentManager().beginTransaction().detach(getTargetFragment()).attach(getTargetFragment()).commit();
                    }
                });
        //TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        //textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }

    @Override
    public void onStart() {
        super.onResume();
        getFragmentManager().beginTransaction().detach(this).attach(this).commit();
    }
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().detach(this).attach(this).commit();
    }

在用户将某个项目设为收藏夹并按后退按钮后,我在列表视图中遇到了同样的问题我希望它对你有用:

当您在片段中打开 activity 时,它将继续暂停,当您按下返回按钮时,将执行 onResume 所以我在 onResume 方法中再次调用了我的方法,如下所示:

@Override
public void onResume() { 
    super.onResume();
    storedfavoritesitem = FavoriteModel.getAllFavorites(); 
    if (storedfavoritesitem.size() == 0) {
        nolist.setVisibility(View.VISIBLE);
        nolist.setText("Tu Lista de Favoritos está vacía ;)");
    }
    if (adapter!=null && currentLocation!=null) {
        mResualt.clear();
        for (int i = 0; i < storedfavoritesitem.size(); i++) {
            FavoriteModel Faveitem = storedfavoritesitem.get(i);
            String locality = Faveitem.name;
            String cityname = Faveitem.city;
            int id = Faveitem.id;
            double lat = Faveitem.lat;
            double lon = Faveitem.lon;
            Location location = new Location("Beach");
            location.setLatitude(lat);
            location.setLongitude(lon);
            float distance = currentLocation.distanceTo(location);
            mResualt.add(new SearchResualtClass(id, distance / 1000, locality, cityname));
        }
        adapter.notifyDataSetChanged();
    }
}

干杯!

假设您有两个活动 A 和 B.Initially 片段 f1 添加到 A.When 您单击 f1 中的项目开始 activity B with startActivityForResult (Intent intent, int requestCode ) 并在 onActivityResult(int requestCode, int resultCode, Intent data) 中更新片段数据(如果可能)或重新加载片段。