title: How to destroy fragment on Back press
body: <p>I have 2 fragments A and B. First opens A fragment, there is button. <code>onButtonClick</code> it goes to fragment B. In fragment B the user selects some languages, or something else and <code>onButtonClick</code> comes to fragment A. So when it comes to Fragment A and i click back on phone, it goes back again on Fragment B. I want that when the user in Fragment B selects some information, languages and other things and then clicked next button, it destroyed. Something like this. When it goes from A to B, then after selecting information goes from B to A i want to destroy the B fragment. In fragment i can't to override the <code>onBackPressed</code> method because this is for activity. Any ideas? Found in internet some samples but they don't helped. If you need the code please say. Thanks for help.</p>
  
  <p>Ok here's the Fragment A.</p>
  
  <pre><code>public class PersonalInfoFragment extends BaseFragment {
  
  View mainView;
  
  public static final String TEXT = "TEXT";
  
  TextView dots;
  EditText skills_description;
  Button clear, save, languages;
  
  CircleImageView circleImageView1, circleImageView2, circleImageView3,
          circleImageView4, circleImageView5, circleImageView6;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      mainView = inflater.inflate(R.layout.personal_info, container, false);
      init(mainView);
  
      Bundle bundle = getArguments();
      if (bundle != null) {
          ArrayList&lt;Integer&gt; s = bundle.getIntegerArrayList("IMAGE");
          if (s != null) {
              for (int i = 0; i &lt; s.size(); i++) {
                  switch (i) {
                      case 0:
                          circleImageView1.setImageResource(s.get(i));
                          break;
                      case 1:
                          circleImageView2.setImageResource(s.get(i));
                          break;
                      case 2:
                          circleImageView3.setImageResource(s.get(i));
                          break;
                      case 3:
                          circleImageView4.setImageResource(s.get(i));
                          break;
                      case 4:
                          circleImageView5.setImageResource(s.get(i));
                          break;
                      case 5:
                          circleImageView6.setImageResource(s.get(i));
                          break;
                      default:
                          break;
                  }
              }
          }
      }
      return mainView;
  }
  
  
  private void init(View v) {
      skills_description = v.findViewById(R.id.skills);
      clear = v.findViewById(R.id.clear);
      languages = v.findViewById(R.id.select_language);
      dots = v.findViewById(R.id.dots);
      save = v.findViewById(R.id.save);
  
      circleImageView1 = v.findViewById(R.id.language1);
      circleImageView2 = v.findViewById(R.id.language2);
      circleImageView3 = v.findViewById(R.id.language3);
      circleImageView4 = v.findViewById(R.id.language4);
      circleImageView5 = v.findViewById(R.id.language5);
      circleImageView6 = v.findViewById(R.id.language6);
  
      clear.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              skills_description.setText("");
          }
      });
  
      languages.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              getFragmentManager().beginTransaction().replace(R.id.container,
                      new FragmentLanguage()).addToBackStack(null).commit();
          }
      });
  
      save.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
          }
      });
  }}
  </code></pre>
  
  <p>On <code>languages.setOnClickListener</code> it goes to Fragment B.</p>
  
  <pre><code>languages.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              getFragmentManager().beginTransaction().replace(R.id.container,
                      new FragmentLanguage()).addToBackStack(null).commit();
          }
      });
  </code></pre>
  
  <p>Here's the Fragment B.</p>
  
  <pre><code>public class FragmentLanguage extends BaseFragment {
  
  public static final String TEXT = "TEXT";
  
  
  MyAdapter adapter;
  
  View mainView;
  ListView listView;
  
  Button next;
  
  PersonalInfoFragment info = new PersonalInfoFragment();
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      mainView = inflater.inflate(R.layout.listview, container, false);
      listView = mainView.findViewById(R.id.list_view);
  
      fillData();
      showResult();
  
      return mainView;
  }
  
  
  public void fillData() {
  
      ArrayList&lt;Item_obj&gt; objS = new ArrayList&lt;&gt;();
  
      //Array list of Languages
      objS.add(new Item_obj("Armenian", R.drawable.download, false));
      objS.add(new Item_obj("Russian", R.drawable.russian, false));
      objS.add(new Item_obj("US English", R.drawable.usa, false));
      objS.add(new Item_obj("Portugal", R.drawable.france, false));
      objS.add(new Item_obj("Spanish", R.drawable.italy, false));
      objS.add(new Item_obj("Georgian", R.drawable.download, false));
      objS.add(new Item_obj("French", R.drawable.download, false));
      objS.add(new Item_obj("Italian", R.drawable.download, false));
      adapter = new MyAdapter(getActivity(), objS);
      listView.setAdapter(adapter);
  }
  
  public void showResult() {
      next = mainView.findViewById(R.id.next);
      clear = mainView.findViewById(R.id.clear_lang);
  
      next.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              ArrayList&lt;Item_obj&gt; it = adapter.getCheck();
  
              Bundle bundle = new Bundle();
              ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();
              for (int i = 0; i &lt; adapter.getCheck().size(); i++) {
                  Item_obj items = it.get(i);
                  if (items.isSelected) {
                      list.add(items.image);
                  }
              }
  
              if (list.size() &gt; 6) {
                  Toast.makeText(getActivity(), "You can't select more than 6 elements", Toast.LENGTH_SHORT).show();
                  list.clear();
              }
  
              bundle.putIntegerArrayList("IMAGE", list);
              info.setArguments(bundle);
  
              getFragmentManager().beginTransaction().replace(R.id.container, info).addToBackStack(null).commit();
              listView.setAdapter(adapter);
          }
      });
  }}
  </code></pre>
  
  <p>In <code>next.setOnClickListener</code> it goes back again to fragment A, with selected languages.</p>