I've searched around but have been unable to find a solution to this problem. I have a Fragment that is being shown and when you press a button it opens a new Activity on top of that. Then when that Activity is finished it returns some intent data back to the Fragment/Parent Activity. That data will sometimes change a TextView or add a button to the Fragment. However, I haven't been able to get the Fragment's view to update without re-adding the Fragment to the backstack (popBackStack then replace or add).
A couple things I've tried is to call invalidate()
and requestLayout()
on the Fragment's base layout but the only way to refresh the view seems to have it call the onCreateView()
again by removing and re-adding it. Is there a way to specify the Fragment's "content view" without reloading it?
Parent Activity code:
// Used to retrieve the data from the first Activity
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
// Try to update the Fragment's view here
mMyFragment.refreshView(data.getString(NEW_BUTTON_TEXT));
}
}
Fragment code (MyFragment):
private View mBaseLayout;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mBaseLayout = inflater.inflate(R.layout.my_layout, container, false);
...
return mBaseLayout;
}
public void refreshView(String newButtonText) {
Button button = mBaseLayout.findViewById(R.id.new_button);
button.setText(newButtonText);
button.setVisibility(View.VISIBLE);
// TODO Reset the Fragment's view with the update mBaseLayout view
// ? ? ?
// Tried mBaseLayout.invalidate() and mBaseLayout.requestLayout() here
}
Any thoughts on this would be really helpful, thanks!