title: Android (java): OnBackPress() closes my app when trying to go back from Activity to fragment
body: <p>So far I had gone from fragment to the DetailActivity, now I added back button to take me back to the previous fragment. I tried the below code but the app keeps closing instead of going back to the fragment.
  Both of these code snippets are from my code.</p>
  <p>Fragment.java</p>
  <pre><code>public class Fragment extends Fragment implements LoaderManager.LoaderCallbacks&lt;Cursor&gt; {
  
  
      String[] days = {&quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;, &quot;Sunday&quot;};
      
      private Button btnNext;
     
  
  
      public Fragment() {
          // Required empty public constructor
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.fragment, container, false);
      }
  
      @Override
      public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
          btnNext = view.findViewById(R.id.add);
          btnNext.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent intent = new Intent(getContext(),
  DetailActivity.class);
          startActivity(intent);
          getActivity().onBackPressed();
              }
          });
      }
  
      @Override
      public Loader&lt;Cursor&gt; onCreateLoader(int id, Bundle args) {
          return null;
      }
  
      @Override
      public void onLoadFinished(Loader&lt;Cursor&gt; loader, Cursor data) {
  
      }
  
      @Override
      public void onLoaderReset(Loader&lt;Cursor&gt; loader) {
  
      }
  </code></pre>
  <p>And this is my onBackPressed() from DetailActivity.java method:</p>
  <pre><code>     public void onBackPressed(){
                  FragmentManager fm = getFragmentManager();
                  if (fm.getBackStackEntryCount() &gt; 0) {
                      Log.i(&quot;DetailActivity&quot;, &quot;popping backstack&quot;);
                      fm.popBackStack();
                  } else {
                      Log.i(&quot;DetailActivity&quot;, &quot;nothing on backstack, calling super&quot;);
                      super.onBackPressed();
                  }
              }
  </code></pre>
  <p>DetailActivity.java :</p>
  <pre><code>    public class DeatilActivity extends AppCompatActivity {
  
     
      
      Button btnBack;
      
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_detail);
  
          
         
          btnBack = findViewById(R.id.btnBack);
  
          btnBack.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
      
                  onBackPressed();
  
              }
          });
  
      }
  
    
     /* @Override
      public void onBackPressed() {
          if (getFragmentManager().getBackStackEntryCount() != 0) {
              getFragmentManager().popBackStack();
          } else {
              super.onBackPressed();
          }
      } */
  
      @Override
      public void onBackPressed(){
          FragmentManager fm = getSupportFragmentManager();
          if (fm.getBackStackEntryCount() &gt; 0) {
              Log.i(&quot;DetailActivity&quot;, &quot;popping backstack&quot;);
              fm.popBackStack();
          } else {
              Log.i(&quot;DeatilActivity&quot;, &quot;nothing on backstack, calling super&quot;);
              super.onBackPressed();
          }
      }
  
  }
  </code></pre>
  <p>Any idea ? :(</p>