我在一个标签式活动中有一个片段。它从onclicklistener重定向到一个活动。在该活动中进行了更改。用户将按下按钮。所以在我的片段中我需要一个从该活动回来后被触发的监听器或者也许在用户做出更改后,我给用户一个按钮,回到那个片段并同时重新加载片段。我不知道哪种方式。 我尝试onResume()onStart(),因为在该事务中片段没有任何反应。 我尝试了addOnBackStackChangedListener()但它应该放在活动而不是片段中。 所以我不知道该怎么办? 谢谢你的时间
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();
}
答案 0 :(得分:1)
用户将项目设为收藏后我对列表视图有同样的问题,然后按回按钮我希望它对你有用:
当您在片段中打开一个活动时,它将继续暂停,当您按下Back按钮时,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();
}
}
喝彩!
答案 1 :(得分:1)
假设您有两个活动A和B.最初将片段f1添加到A.当您使用startActivityForResult(Intent intent,int requestCode)和onActivityResult(int)单击f1 start activity B中的item时requestCode,int resultCode,Intent data)更新片段数据(如果可能)或重新加载片段。