Save only first fragment on Android back stack -


For some time I have problems with pieces and back stacks. I have an activity in my app - MainActivia with drawers and a few pieces.

My main activity looks something like this:

  increases public class main activity actionbearactivity {Private Static Final Ent HOME_NUM = 0; Private static final integer ABOUT_NUM = 1; Private boolean first fragment change = false; @ Override Protected Zero (Bundle Saved Instantstate) {Super. NET (Saved Instantstate); SetContentView (R.layout.activity_main); GetSupportActionBar () setHomeButtonEnabled (true) // Display some other initalizations (HOME_NUM); } Private Waste DisplayFragment {piece of fragment = zero; Switch (position) {case HOME_NUM: section = new main profit (); break; Case ABOUT_NUM: fragment = new aboutFragment (); break; Default: Break; } Piece Manager Piece Manager = getSupportFragmentManager (); Piece Transaction Piece Transaction = Piece Manager. Section Transaction: Raleigh (RID frame); Container, piece); If (! First fake changes) {fragmentTransaction.addToBackStack (null); } FragmentTransaction.commit (); Change the first fragment = true; }}   

MainFragment class looks like this:

  The public class implements the MainListFragment ListFragment LoaderManager.LoaderCallbacks & lt; Cursor & gt; {Private Static Last Full AD AD = 0; @ Override Public View Crate View (LayoutInflator Inflator, View Group Container, Bundle Saved InstantState) {View View = inflater.inflate (R.layout.fragment_list_main, Container, Incorrect); setHasOptionsMenu (true); Return view; } @CreateOptions Override public zeros on menu (menu menus, menu inflator inflator) {inflater.inflate (R. menu.Man_frames_ab, menu); Super.onCreateOptionsMenu (menu, inflater); } @ Override Public Boolean On Option It's Select (Menu Item) {Switch (ITAT. ITMID)} {Case R.ID.action_ed: DisplaysFragment (ADD); break; Default: Break; } Return Super.Options item selected (item); } Private Waste DisplayFragment {piece of fragment = zero; Switch (position) {add case: piece = new addFragment (); break; Default: Break; } Piece Manager Piece Manager = getFragmentManager (); Piece Transaction Piece Transaction = Piece Manager. Section Transaction: Raleigh (RID frame); Container, piece); fragmentTransaction.commit (); }}   

What I want and what I am trying to achieve is the first (initial) piece on my back, even though that is MainFragment It does not work even after adding main activity to addToBackStack () << p>

Now I have this stap on my back The add piece on the heap is doubled.

Perhaps to better describe this:

Now:

  start -> Main Activity (Displays) Main Fragment - & gt; (Press button) - & gt; AddFragment (pressed back) - & gt; AddFragment (still stays) - & gt; App app   

How do I want to work it out:

  start - & gt; Main Activity (Displays) Main Fragment - & gt; (Press button) - & gt; AddFragment (pressed back) - & gt; Mainfragment - & gt; Can anyone tell me how can I get it?   

You always get a new piece instance by pressing your button. Tag them better and see if they are already around. In this way you will always keep an example of your specific piece and your problem will end.

In addition, if you want to check that a piece has already been added to the backstacks use, then Fragment.getBackStackEntryCount () . If this returns, there is no piece in the backstack.

Something like this, maybe you have to modify it a little bit, but you should get this idea:

  Private zero displayfragment (piece of intro) {piece piece = null; Piece Manager Piece Manager = getSupportFragmentManager (); Piece Transaction Piece Transaction = Piece Manager. Switch (position) {case HOME_NUM: piece = piece MANAGER.findFragmentByTag (FIRST_TAG); If (piece == tap) {fragment = new MainFragment (); } FragmentTransaction.replace (R.id.frame_container, slice, FIRST_TAG); // Set the tag break; Case ABOUT_NUM: segment = fragment manager.findFragmentByTag (SECOND_TAG); If (piece == faucet) {fragment = new aboutFragment (); } FragmentTransaction.replace (R.id.frame_container, fragment, SECOND_TAG); // Set the tag break; Default: Break; } If (fragmentManager.getBackStackEntryCount == 0) {fragmentTransaction.addToBackStack (null); } FragmentTransaction.commit (); }   

Also make sure this method once! There is no need for an additional method in your piece.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -