Obsolete
Status Update
Comments
al...@google.com <al...@google.com> #2
Closing due to modification date out of supported range.
If this issue is still relevant for the latest libraries and important to address, please file a new issue and it will enter our revamped issue triage process.
If this issue is still relevant for the latest libraries and important to address, please file a new issue and it will enter our revamped issue triage process.
co...@google.com <co...@google.com> #3
Opened a new issue here: b/153082833
Description
Version used: 23.1.1, 23.2.0, 23.3.0
Theme used: Theme.AppCompat.Light
Devices/Android versions reproduced on: Android 6.0, Nexus 5 (MRA58I)
After updating from 23.1.1 to 23.2.0 or 23.3.0, it seems that the behavior of onAttachFragment() has changed when doing a replacement transaction.
With 23.1.1, if I do a FragmentTransaction to replace an existing Fragment with the same ID and tag, then in onAttachFragment(), the provided fragment parameter is the same as what is retrieved using getSupportFragmentManager().findFragmentByTag(TAG) -- the new replacement Fragment.
With 23.2.0 and 23.3.0 using the same code, the provided fragment parameter is the new replacement Fragment, but getSupportFragmentManager().findFragmentByTag(TAG) returns the old Fragment (the one being replaced).
Simple Activity that will recreate the issue:
public class MainActivity extends BaseActivity {
private static final String FRAGMENT_TAG = "fragment_tag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, new StartingFragment(), FRAGMENT_TAG)
.commit();
}
}
@Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
// On 23.1.1, findFragmentByTag() finds the fragment by tag after onAttachFragment().
// On 23.2.0, findFragmentByTag() returns the fragment that is being replaced and this will crash.
if (fragment != getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG)) {
throw new RuntimeException("This shouldn't happen.");
}
}
void replaceFragment() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(android.R.id.content, new ReplacementFragment(), FRAGMENT_TAG);
ft.addToBackStack(ReplacementFragment.class.getName());
ft.commit();
}
public static class StartingFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
Button button = new Button(getContext());
button.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
button.setText("Replace Fragment");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity) getActivity()).replaceFragment();
}
});
return button;
}
}
public static class ReplacementFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = new ImageView(getContext());
view.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
view.setBackgroundColor(Color.BLUE);
return view;
}
}
}