gpt4 book ai didi

android - 刷新在该 Activity 中按下后进入 Activity 的 fragment

转载 作者:行者123 更新时间:2023-11-29 15:04:00 33 4
gpt4 key购买 nike

我在选项卡式 Activity 中有一个 fragment 。它从 onclicklistener 重定向到一个 Activity 。在该 Activity 中进行更改后。用户将按下后退按钮。因此在我的 fragment 中,我需要一个从该 Activity 返回后触发的监听器。或者也许在用户进行更改后,我给用户一个按钮返回到该 fragment 并同时重新加载 fragment 。我不知道哪种方式。我在每件事上都尝试了 onResume() onStart() 不起作用,因为在该事务中没有发生任何 fragment 。我试过 addOnBackStackChangedListener() 但它应该放在 Activity 中而不是 fragment 中。所以我不知道该怎么办?谢谢你的时间

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

最佳答案

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

当你在一个 fragment 中打开一个 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();
}
}

干杯!

关于android - 刷新在该 Activity 中按下后进入 Activity 的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40485586/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com