I'm trying to create artificial backstack after opening notification from notification bar.
Workflow after opening notification:
if(getArguments != null)
. If it's not null I open notifications fragment and pass received notification id.if(id != -1)
I open Notification fragment.The problem is that after clicking back button I go back straight into MainActivity window skipping Notifications fragment.
I open Notifications fragment like that:
Fragment frag = new NotificationFragment();
frag.setArguments(bundle);
FragmentTransaction ft =getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, NotificationsFragment.getInstance(null, id)).commit();
I open Notification fragment like this:
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
NotificationFragment notificationFragment = new NotificationFragment();
Bundle bundle = new Bundle();
bundle.putInt("id", getArguments().getInt("id"));
notificationFragment.setArguments(bundle);
ft.add(R.id.container, notificationFragment).addToBackStack(null).commit();
R.id.container
is FrameLayout used sor showing fragments.
I'm using Support librrary.
I read that FragmentTransaction.replace() deletes all existing fragments and adds new one. And FragmentTransaction.add() adds them to activity state.
Please someone correct me what I'm doing wrong and show me right way.
Change this
Fragment frag = new NotificationFragment();
frag.setArguments(bundle);
FragmentTransaction ft =getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, NotificationsFragment.getInstance(null, id)).commit();
to
Fragment frag = new NotificationFragment();
frag.setArguments(bundle);
FragmentTransaction ft =getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, NotificationsFragment.getInstance(null, id)).addToBackStack(null).commit();
Basically, you are not adding Notifications Fragment to backstack, that's why when clicking back, MainActivity comes up.