amitsalyan
amitsalyan

Reputation: 678

Fragment navigation on back button

I have a fragment which has a context menu which in turn calls another fragment:

switch (item.getItemId())
            {
 case MENU_EDIT:
                 FragmentTransaction ft =getActivity().getFragmentManager().beginTransaction();
                 PlayListDetailsView fragment=new PlayListDetailsView();
                 //fragment.getArguments().putLong("id", (Long)info.id);
                 ft.add(android.R.id.content, fragment);
                 ft.attach(fragment);
                 ft.commit();
}

The new fragment opens alright, but once i press back the app totally exits withour going back to the fragment it was called from. Also the commented line of an attempt to add a bundle information fails and cannot be retrieved in the fragment called Suggest a fix please :)

Upvotes: 0

Views: 639

Answers (2)

Andrey Novikov
Andrey Novikov

Reputation: 5593

First of all fragments should communicate through activity, not directly: http://developer.android.com/training/basics/fragments/communicating.html

As for your question:

  1. For back key to work properly, you should add transaction to back stack, put this before commit:

    ft.addToBackStack("playlistdetails");
    
  2. Arguments should be added like this:

    Bundle args = new Bundle();
    args.putLong("id", (Long)info.id);
    fragment.setArguments(args);
    

Upvotes: 2

sujith
sujith

Reputation: 2421

you have to call addToBackstack() before calling commit.

Upvotes: 3

Related Questions