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 - My What I want and what I am trying to achieve is the first (initial) piece on my back, even though that is Now I have this stap on my back The Perhaps to better describe this: Now: How do I want to work it out: 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 Something like this, maybe you have to modify it a little bit, but you should get this idea: Also make sure this method once! There is no need for an additional method in your piece. MainActivia with drawers and a few pieces.
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 (); }}
MainFragment It does not work even after adding
main activity to
addToBackStack () << p>
add piece on the heap is doubled.
start -> Main Activity (Displays) Main Fragment - & gt; (Press button) - & gt; AddFragment (pressed back) - & gt; AddFragment (still stays) - & gt; App app
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?
Fragment.getBackStackEntryCount () . If this returns, there is no piece in the backstack.
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 (); }
Comments
Post a Comment