Help fix a memory leak when changing screen orientation. Activity contains two fragments, each of which can be deleted by pressing a button. I can not figure out how to eliminate the leak that occurs when changing orientation. As far as I understand it is necessary to somehow fix the fragments in the activation. Memory leak due to re-activation is not important.
MainActivity.java
package com.example.robot.test; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import com.example.robot.test.fragments.ScrollingActivity; import com.example.robot.test.fragments.item1; import com.example.robot.test.fragments.item10; import com.example.robot.test.fragments.item2; import com.example.robot.test.fragments.item3; import com.example.robot.test.fragments.item4; import com.example.robot.test.fragments.item5; import com.example.robot.test.fragments.item6; import com.example.robot.test.fragments.item7; import com.example.robot.test.fragments.item8; import com.example.robot.test.fragments.item9; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { item1 it1; item2 it2; item3 it3; item4 it4; item5 it5; item6 it6; item7 it7; item8 it8; item9 it9; item10 it10; ScrollingActivity scA; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); it1 = new item1(); it2 = new item2(); it3 = new item3(); it4 = new item4(); it5 = new item5(); it6 = new item6(); it7 = new item7(); it8 = new item8(); it9 = new item9(); it10 = new item10(); scA = new ScrollingActivity(); // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); //noinspection SimplifiableIfStatement if (id == R.id.Delete_fragment1) { ft.remove(it8); } if (id == R.id.Delete_fragment2) { ft.remove(it1); } ft.commit(); return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); if (id == R.id.nav_item1) { ft.replace(R.id.containter, it1); } else if (id == R.id.nav_item2) { ft.replace(R.id.containter, it2); } else if (id == R.id.nav_item3) { ft.replace(R.id.containter, it3); } else if (id == R.id.nav_item4) { ft.replace(R.id.containter, it4); } else if (id == R.id.nav_item5) { ft.replace(R.id.containter, it5); } else if (id == R.id.nav_item6) { ft.replace(R.id.containter, it6); } else if (id == R.id.nav_item7) { ft.replace(R.id.containter, it7); } else if (id == R.id.nav_item8) { ft.replace(R.id.containter, it8); // тут и получаю два фрагмента в одном активити ft.add(R.id.containter,it1); } else if (id == R.id.nav_item9) { ft.replace(R.id.containter, it9); } else if (id == R.id.nav_item10) { ft.replace(R.id.containter, it10); } ft.commit(); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } } 1 fragment
package com.example.robot.test.fragments; import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.robot.test.R; /** * A simple {@link Fragment} subclass. * Activities that contain this fragment must implement the * {@link item1.OnFragmentInteractionListener} interface * to handle interaction events. * Use the {@link item1#newInstance} factory method to * create an instance of this fragment. */ public class item1 extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, eg ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; public item1() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment item1. */ // TODO: Rename and change types and number of parameters public static item1 newInstance(String param1, String param2) { item1 fragment = new item1(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment Log.d("qqq","item1 "+this); return inflater.inflate(R.layout.fragment_item1, container, false); } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } @Override public void onDetach() { super.onDetach(); mListener = null; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * <p> * See the Android Training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >Communicating with Other Fragments</a> for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } } 2 fragment
package com.example.robot.test.fragments; import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.robot.test.R; /** * A simple {@link Fragment} subclass. * Activities that contain this fragment must implement the * {@link item8.OnFragmentInteractionListener} interface * to handle interaction events. * Use the {@link item8#newInstance} factory method to * create an instance of this fragment. */ public class item8 extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, eg ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; public item8() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment item8. */ // TODO: Rename and change types and number of parameters public static item8 newInstance(String param1, String param2) { item8 fragment = new item8(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment Log.d("qqq","item8 "+this); return inflater.inflate(R.layout.fragment_item8, container, false); } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } @Override public void onDetach() { super.onDetach(); mListener = null; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * <p> * See the Android Training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >Communicating with Other Fragments</a> for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } }
scA = new ScrollingActivity();- Yuriy SPb ♦