Reputation: 151
I have a fragment in a tabbed activity.It redirects from onclicklistener to an activity.After changes made in that activity.User will press back button.So in my fragment i need a listener that is fired after come back from that activity.Or maybe after user made the changes I give user a button to go back to that fragment and reload the fragment meanwhile.I don't know either way. i tried onResume() onStart() on every thing didn't work because nothing happened to fragment in that transaction. I tried addOnBackStackChangedListener() but it should be placed in the activity not fragment. so i don't know what to do? thanks for your time
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();
}
Upvotes: 2
Views: 5184
Reputation: 1872
Let's say you have two two activities A and B.Initially Fragment f1 is added to A.When you click on item in f1 start activity B with startActivityForResult (Intent intent, int requestCode) and in onActivityResult(int requestCode, int resultCode, Intent data) either update the fragment data(if possible) or reload the fragment.
Upvotes: 2
Reputation: 331
i had the same problem for a list view after user made an item as favorite and press back button i hope it work for you:
When you open an activity inside a fragment it will go on Pause and when you press Back button the onResume will be executed so I called my method once more in onResume method like this:
@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();
}
}
cheers!
Upvotes: 4