I read that retaining fragment with ui and references on views can cause memory leaks. Than I create test app with fragment where I store some references on views and set setRetaineInstance(true), but several screen rotation does not cause any leaks. MAT says that I have only one instance of parent activity. What I'm doing wrong? In which cases retaining fragment with ui can cause leaks?
RetainInstanceActivity.java
public class RetainInstanceActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, RetainFragment.newInstance())
.commit();
}
}}
RetainFragment.java
public class RetainFragment extends Fragment {
private View mLogin;
private View mPassword;
private View ImageView;
public static RetainFragment newInstance() {
final RetainFragment fragment = new RetainFragment();
return fragment;
}
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(final LayoutInflater inflater,
final ViewGroup container, final Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_retain, container,
false);
mLogin = view.findViewById(R.id.login);
mPassword = view.findViewById(R.id.password);
ImageView = view.findViewById(R.id.img);
return view;
}
}