Prevent last Fragment from being removed from backstack - android

I am currently writing a drawer layout as my main layout, with an embedded FrameLayout that I will be using to hold each “page” when an item on the drawer is clicked on. When the app first starts, an initial fragment will be show. Other fragments may be added/replaced later which is fine, however, my problem is that when the user clicks the back button on the very first “initial fragment”, I don’t want that particular fragment to be removed from the layout. Currently, it is being removed and it’s just showing a drawer layout with no other content (which makes sense). I want the app to automatically exit if the initial fragment was the last one showing and the back button is pressed, instead of removing that initial fragment and then after another back press, then it exits.
Things I have thought of doing:
Not adding the first fragment to the backstack. (If I do this, I can compare it with the classname of the fragment which is a somewhat longer string, or I can use a boolean value for once the first fragment has been placed (and not added to backstack), the boolean is set which allows the fragments to now be added.
Overriding the onBackPressed function of the activity
Does anyone have a suggested way of doing this or can think of a better way? Thanks

The first bullet point sounds the cleanest. You have no other need to handle conditions when back is hit, correct? If that's the case, it's less lines of code (removing one as opposed to adding several) and you get to keep default Activity methods as is.
I know that's not exactly what you asked, but I think the first bullet point is so clean, that I just wouldn't try something else.

I have implemented same in one of the app using my own Stack of fragment. and also implemented onBackPressed method.
Every time when user clicks on item in drawer i add fragment in stack and in back press once its length is 1 I finish the activity with message.
On item click -- Add/replace fragment in container.
OnBackPressed -- Pop fragments from stack and once its last one i finish activity.
Hope this can give you another option to consider.

Related

Android fragments refresh every time after returning back to them

This has to be really simple but I've never worked with fragments before and I'm lost here.
I searched and found tons of stuff on fragmets but I can't find an answer to my problem.
On my main activity I have a submenu with 5 buttons, basically a LinearLayout.
I have a List with 5 fragments in it.
When I press a button, I want to have one of the five fragments to be seen.
If I add the fragments with FragmentTransaction.replace(), the fragments are recreated every time. Everything refreshes and this is not what I want. Views are also refreshed if I return to a fragment by pressing the back button.
I couldn't figure out how "not to refresh" fragments. I tried using hide/show but I lose the track of the backstack at some point hence I couldn't iplement the back button behaviour.
Any help would be much appreciated. Thanks.
I think you mean FragmentTransaction.replace(). Consider editing your question.
Anyway, the view will always be recreated when you change a fragment. It is your job to keep the information needed in a Bundle by implementing onSavedInstanceState() and then retrieving the info in onCreateView().

Android Two layout for one activity

I have a form, and I have chosen to divide it in two steps.
To do it, I created two layouts for a same activity. When the user complete the first form, I call the second layout with :
setContentView(R.layout.activity_form2);
The problem is, if the user wants to come back at the first step of the form, it's not running, because he comes back to the previous activity.
Is it correct to do that, or I need to use fragment?
Otherwise, how can I do to come back on the previous layout, and not the previous activity?
Never set different layout's for the same Activity. You can navigate to a different Activity or you could use Fragments.
Layout is set to the Activity and when you click back button Activity is popped from the back stack and previous Activity in the stack takes focus. So setting different layouts for the same Activity is not a good choice.

Navigate up to last shown fragment

I've implemented a NavigationDrawer within the MainActivity which containts two Fragment. The second Fragment contains a ListView that opens a new Activity (onItemClick) that shows the according detailed data. (I guess that's pretty much the master/detail flow).
When I use the up button to navigate back I got shown the first fragment and not the second fragment with the ListView.
Any ideas how to solve that problem?
Make method in MainActivity for example setFragment(int whichFragment);
and set fragment you want in it, you should already have code that do that and than call that method in onBackPressed() method.
For your question about another fragment, well it depends on how your master/detail flow is suppose to work, it is not a problem to use another activity if you dont need left menu any more, but if you do need left menu then use another fragment.
Best regards

FragmentStatePagerAdapter fails to load the fragments again when I get back to old instance of the parent Fragment

Scenario:
I have a fragment which has a ViewPager which contains 5 instances(different) of a single Fragment with different values, each having a listivew that contains some sort of items.
Problem:
The flow goes smooth when I click an item of listView(say on page1) but the moment I come back from there on pressing back (overridden OnBackPressed in the main_activity (will discuss later)), the viewPager fails to load the subFragments.
But when I switch to the 2nd or 3rd page, it gets displayed, and on going back to 1st page, it now gets displayed.
OnBackPressed():
I am maintaining a manual stack here. When I click on an item of ListView, the current Fragment Instance (of the parent Fragment ofcourse) goes into stack. And when user comes back , that instance gets popped off the stack and I replaces it on the activities FrameLayout container.
References:
Fragments not getting recreated by FragmentStatePagerAdapter after coming back to the main Fragment instance
Guys, I am really pissed off here, please help me out.
Frankly, I haven't worked much with fragments.
But it seems like the page is being thrown off the memory every time you switch to other pages. I am not sure but if there is a way for android to manage this and keep the page in its memory persistently, it might solve the problem
Finally I am able to get a workaround for this issue, which included two things:
while replacing the fragment make a call to
ft.replace(R.id.content_frame, frag).addToBackStack(null);
while initiating the ViewPager, the fragment manager to be used is :
mAdapter = new myAdapter(getChildFragmentManager());
Actually the problem is that, android fails to recognise and load the older instance of the fragment unless you explicitly add that instance to the backstack. So after adding it to BackStack, there is practically no need to maintain your own manual Stack, but you still can do that.

How to add call a callback in the Activity when there is a Fragment pushed from back stack

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.

Categories

Resources