I read that a persistent snippet with ui and links to views can cause a memory leak. Than I create a test application with a fragment, where I store some links to the views and set setRetaineInstance (true), but a few screen rotations do not cause any leaks. MAT says that I have only one instance of parent activity. What am I doing wrong? In what cases can saving a fragment with ui lead to 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; }
}
android memory-leaks android-fragments
Bracadabra
source share