android - Prevent Fragments from being added multiple times to back stack -


I am currently laying the basics for an application that I am going to develop. There is an action bar with some menu items in it, each menu has its own piece of item, which must be displayed in main activity. However, I need the "above" interest of the action bar, when any piece but the mainfragrame itself is displayed.

My current approach is based on the solution presented and something looks like this: @ Override public booleanon option item selected (menu items) {int id = item GetItemId (); Piece piece = null; If (id == RID.action_identities) {fragment = new IdentitiesFragment (); } And if (id == R.id.action_history) {fragment = new HistoryFragment (); } And if (id == R.id.action_settings) {fragment = new SettingsFragment (); } And if (id == R.id.action_about) {fragment = new aboutFragment (); } If (piece! = Null) {Fragment Transaction Transaction = getFragmentManager (). BeginTransaction (); Transaction.replace (R.id.container, fragment); Transaction.addToBackStack (zero); Transaction.commit (); Back true; } Else {return super.onOptionsItemSelected (item); }} Override public null at @BackStackChanged () {displayHomeAsUp (); } Private zero performance homeup () {int stackCount = getFragmentManager () GetBackStackEntryCount (); GetActionBar () SetDisplayHomeAsUpEnabled (StackCount> gt; 0); } @ Override Public Boolean On Navigating Up () {getFragmentManager (). PopBackStack (); Back true; }

It works great, but there is a problem when pressing the same menu item several times, because every time a new charge will be placed on a piece of piece. What is the best way to stop it from happening? Obviously I can check whether the present piece is being requested or not, but it results in many checks and it is redundant, the other way can be the transaction tag, but I'm not sure that the cleaner The code will be.

What is the best way to do this? This is not a common problem, or the way I want to make the app wrong "behavior"? Because personally I do quite a lot like the "up" function of the action bar.

You can check its simple and clean by boolean ... <4 p>

That is, you have 4 pieces, so each bullion check is required, when you press a piece, its corresponding boolean will be correct and next time you press it until you backstack it until it is used to add the piece Is ...

  if (intro == true & amp; id == RID.action_digitation) {fragment = new identitiesFragment (); Identity = true; } And if (history == true & amp; id == RID. activity_history) {fragment = new HistoryFragment (); History = true; } And if (setting == true & amp; id == RID.action_settings) {fragment = new settingsFragment (); Setting = true; } And if (about == true & amp; id == RID.action_outout) {fragment = new aboutFragment (); About = true; }    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -