简体   繁体   中英

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 :)

您必须在调用提交之前调用addToBackstack()

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM