title: Second Activity back press button does not navigate to Fragment that started activity
body: <p>I am learning android and I am facing a challenge no idea on how to resolve it. That is am unable to go back to the fragment that started the activity. I am opening second activity from fragment but when I press the back button on second activity it does not navigate to the fragment that opened the activity,it shows the home fragment. I have tried <a href="https://stackoverflow.com/questions/31491093/how-to-go-back-to-previous-fragment-from-activity">this</a> and no success.
  <a href="https://i.stack.imgur.com/aY7rS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aY7rS.png" alt="home fragment" /></a></p>
  <p>and this is the chat fragment that is opening the second fragment</p>
  <p><a href="https://i.stack.imgur.com/DA8ZC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DA8ZC.png" alt="chat fragment" /></a></p>
  <p>and this is the second activity which if I press back button does not go to chat fragment</p>
  <p><a href="https://i.stack.imgur.com/CdJhR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CdJhR.png" alt="enter image description here" /></a></p>
  <p>and when I press the back button of the second activity this is what am getting. Its showing home fragment content</p>
  <p><a href="https://i.stack.imgur.com/AAguT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AAguT.png" alt="enter image description here" /></a></p>
  <p>Here is my Main Activity</p>
  <pre><code>public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
      Fragment fragment;
      Toolbar toolbar;
      TextView title,txt_profile_contact,txt_profile_name;
      ActionBarDrawerToggle toggle;
      DrawerLayout drawer;
     
      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
         
          setContentView(R.layout.activity_main);
  
          notificationRepository=new NotificationRepository(this);
          toolbar=findViewById(R.id.toolbar);
          title=toolbar.findViewById(R.id.tool_bar_title);
          title.setText(R.string.home);
  
  
          setSupportActionBar(toolbar);
  
          getSupportActionBar().setDisplayShowTitleEnabled(false);
  
          drawer= findViewById(R.id.drawer_layout);
          toggle = new ActionBarDrawerToggle(
                  this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
          drawer.setDrawerListener(toggle);
          toggle.syncState();
  
          NavigationView navigationView =  findViewById(R.id.nav_view);
          navigationView.setNavigationItemSelectedListener(this);
  
     
      }
      
       @Override
      public void onBackPressed() {
  
        
          DrawerLayout drawer = findViewById(R.id.drawer_layout);
          assert drawer != null;
  
          if (drawer.isDrawerOpen(GravityCompat.START)) {
              drawer.closeDrawer(GravityCompat.START);
              return;
          }
          getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
  
          doExit(false);
  
  
      }
      
      
       @Override
      public boolean onNavigationItemSelected(MenuItem item) {
          // Handle navigation view item clicks here.
          int id = item.getItemId();
  
          Fragment fragment=null;
          int idd=R.id.main_fragment_main;
          if (id == R.id.menu_chat)
          {
              title.setText(&quot;Chat&quot;);
              fragment=new ChatFragment();
             
  
          } else  (id == R.id.menu_home) {
              title.setText(&quot;Home&quot;);
              fragment=new HomeFragment();
              Utils.setIsOnChatActivity(this,false);
  
  
          }  
  
  
          DrawerLayout drawer = findViewById(R.id.drawer_layout);
          drawer.closeDrawer(GravityCompat.START);
          load_fragment(fragment);
  
          return true;
      }
  
      private void load_fragment(Fragment fragment)
      {
          if(fragment==null)
          {
              return;
          }
          FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  
          ft.setCustomAnimations(R.anim.fadein, R.anim.fadeout);
                         toggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));
                  toolbar.setBackgroundResource(R.color.colorPrimary);
                title.setTextColor(getResources().getColor(R.color.white));
  
  
          try {
                  ft.replace(R.id.main_fragment, fragment)
                  .addToBackStack(null)
                  .commit();
      } catch (IllegalStateException e) {
         //ExceptionHandler.logException(e);
      }
  
  
  
  
      }
  
      }
  </code></pre>
  <p>and here is the chat fragment</p>
  <pre><code>public class ChatFragment extends Fragment implements TutorListAdapter.TutorOnclickListener{
      private static final String TAG = ChatFragment.class.getSimpleName();
  
   
  
  
      public ChatFragment() {
          // Required empty public constructor
      }
  
     
      FloatingActionButton actionButton;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          View view= inflater.inflate(R.layout.fragment_chat, container, false);
        
          actionButton=view.findViewById(R.id.new_chat_list_btn);
  
  
          actionButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent intent=new Intent(getActivity(),SecondActivity.class);
                  startActivity(intent);
                  mActivity.overridePendingTransition(R.anim.fadein, R.anim.fade_out);
              }
          });
  
  
  
  
          return view;
      }
      }
  </code></pre>
  <p>and here is my second activity. NB I have tried even the commented code and does not work</p>
  <pre><code>public class SecondActivity extends AppCompatActivity {
  
      Toolbar toolbar;
      TextView barText;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_subject_details);
          toolbar=findViewById(R.id.custom_tool_bar);
          barText=toolbar.findViewById(R.id.toolbar_title);
          barText.setText(&quot;My Activity&quot;);
          setSupportActionBar(toolbar);
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
          getSupportActionBar().setDisplayShowHomeEnabled(true);
          getSupportActionBar().setDisplayShowTitleEnabled(false);
  
      }
  
  //    @Override
  //    public boolean onOptionsItemSelected(MenuItem item) {
  //        if (item.getItemId() == android.R.id.home) {
  //            onBackPressed();
  //            return true;
  //        }
  //        return super.onOptionsItemSelected(item);
  //    }
  //
  //    @Override
  //    public void onBackPressed() {
  //        Toast.makeText(this, &quot;back pressed?&quot;, Toast.LENGTH_SHORT).show();
  //        if ( getFragmentManager().getBackStackEntryCount() &gt; 0)
  //        {
  //            getFragmentManager().popBackStack();
  //            return;
  //        }
  //        super.onBackPressed();
  //    }
  @Override
  public boolean onSupportNavigateUp(){
      finish();
      return true;
  }
  }
  </code></pre>