First, redefine the backpressure to go to the activity in which the fragments are located: -
@Override public void onBackPressed() { Intent intent = new Intent(CurrentActivity.this,ActivityYouLikeToGo.class); intent.putExtra("Check",1); startActivity(intent); }
then go to the file ActivityYouLikeToGo.java and in onCreate do the following: -
Intent intent = getIntent(); String s1 = intent.getStringExtra("Check"); if(s1.equals("1")) { s1 = ""; Fragment fragment = new YOURFRAMENTNAME(); if (fragment != null) { FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frame_container, fragment).commit(); } }
source share