سؤال

please give me advice how to handle following situation.. I have a main Activity with navigation drawer..When the user clicks on items in the drawer, I change currently attached fragment with another one - accordingly to the pressed drawer item. I also want to handle the situation when the user clicks on the item related to at the same time already attached fragmen and avoid fragment recreate..

So I came up with following solution. I have an property in which I hold the TAG of my current fragment. When the user clicks on any item form the drawer I check the if TAGs are matching and of not I do a switch..I works.

But I have an problem with back stack navigation. I dont know how to change the TAG holding property when the user clicks on the back button. The fragment changes properly, but the TAG property stays the same, so that it all becames broken (when the user clicks on the item he was before, he is not redirected and furthemore after click on the item related to the fragment pushed from back stack it does the recreate:/)

Hope you guys got where is my problem..I dont give here any code. I just think its not necessary, because my problem is not actually in an existing part of my code, but in a hypothetical yet nonexisting one:) I just need to handle the pushed-from-backstack situation..

Thanks in advance!

هل كانت مفيدة؟

المحلول

no need to callback anything!

let's say for example that the layout where your fragments get replace on each transaction is R.id.mycontent

so instead of having the TAG in a field you can, on each click on the drawer, do this:

String currentTag = getSupportFragmentManager.findFragmentById(R.id.mycontent).getTag();

and with this one you compare to what was clicked.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top