2

I am loading an activity from my fragment. If I press the back button on the device, it goes back to the Gridfragment (as expected).

But, if I click the action bar back arrow, it goes back to the mainActivity hosted fragment = HomeFragment and not the Gridfragment.

From HomeFragment - ( default fragment)

Fragment f = GridFragment.newInstance(GridFragment.TAG_bundlePhotosList);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).addToBackStack(null).commit();

Fragment Grid:

Intent i = new Intent(getActivity(), ImagePreviewActivity.class);
i.putExtra("from_grid", "from_grid");
tartActivity(i);

ImagePreviewActivity :

//Display back arrow
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(ImagePreviewActivity);
Abhinav Arora
  • 537
  • 1
  • 7
  • 23
Thiago
  • 12,778
  • 14
  • 93
  • 110

1 Answers1

0

This is the same problem, and as such the same solution as here.

When you use UP navigation, then the previous activity is recreated. To prevent that from happening while you preserve the UP navigation, you can get the intent of the parent activity, and bring it to front if it exists, otherwise create it if not.

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428