Single activity Fragment based app and back button - android

I got the app, that uses practcally just one activity. There is main area where I put fragments into. But what about back button now?
Of course I can override onBackPressed() but with what?
I'm pushing a fragment into it's holder using FragmentTransaction.replace() method every time. I might be lacking understanding of a subject, but shouldn't there be some fragment built-in stack, that would allow me to point onBackPressed() to the previous fragment in stack?

Do not override onBackPress. You can add any fragment transaction to back stack. There is special method to do this.

Related

Android going back to previous activity instead of deleting fragment?

I have 2 activities with fragments inside of them. Whenever I press the back button, the fragment gets destroyed (Used FragmentTransaction's replace method for adding them into the activity) instead of going back to the first activity immediately.
How can I achieve the behavior I want to have?
You can override onBackPressed method in your activity and check if the fragmentManager.getBackStackEntryCount()==0. If yes finish() the activity.
Hope it helps.

Force onDestroyView on a fragment's view

I'm writing an app that loads user profiles and user's ratings for different places. The app uses fragments pervasively, and it's relatively easy to jump from a profile to a rated place.
As a user clicks a profile and gets to a rated place, they can click another profile on a rated place and go on and on.
The problem I'm having is memory related, when I'm looking at a ranked place and I click a profile, I switch from one Activity to the next. In both of these activities, after setContentView I load a fragment dynamically into layout space.
Now, as I shuttle between these activities, onSaveInstance state is almost always called, however since the Fragment displaying whatever was in the foreground before the activity switch, onDestroyView is not called.
What I would like is when onSaveInstanceState is called in these dynamic fragments, it to force onDestroyView to be called as well.
The only time onDestroyView seems to be called is when I add a Fragment to the back stack. When another activity comes to the foreground and this fragment is stopped, I'd like on DestroyView to be called as well.
The current workaround I want to implement is have an empty fragment with no view, and every time I call startActivity(Intent i), load this dummy fragment to destroy views and start the next activity. Then, when I come back, pop it off the back stack and restore the actual last fragment.
This however seems excessive. So, for a stopped fragment in a stopped activity with a new activity in front of it, how do I force it to destroy it's View?
First, you should not force or satisfy onDestroyView to fix your code, that's the job of the FragmentManager and the Android lifecyle # Pausing and Resuming an Activity. If you want to work with your existing code, use the other override methods onPause() or onStop().
Without posted code, I assume you're using the replace() method to display one fragment over another. This more or less forces you to manage the fragments yourself, some developers actually succeed in doing so with some struggle (look at other SO questions).
So my suggestion for you is either:
Maintain your own states, and show the proper fragments based on the state.
Use the BackStack and let the Fragment management handle the stack/states.

Can I put a fragment on Backstack which hasn't been initialized yet?

I want to remove a fragment from backstack and put another fragment in its place which hasn't been created yet. Below is a picture of what I want to achieve.
Is there any way to accomplish this?
EDIT:
As pointed out by #Elltz, it's not possible. So my question becomes
Is it possible to destroy the last fragment from back stack?
Can I handle the back button pressed manually to provide a different fragment than what is on the back stack?
NO! NO! NO !
want to remove a fragment from backstack and put another fragment
you can not manually re-arrange the BackstackEntry. you can only listen,observe and call it. look into PopBackstack(String name, int flags) hence the first no
and put another fragment in its place which hasn't been created yet
you mean a null Fragment ?.. you can not also assign references to null object, the second no
Is there any way to accomplish this?
A confirmation No.
EDIT
YES! YES! YES!
Is it possible to destroy the last fragment from back stack?
you can; using FragmentManager & FragmentTransaction
FragmentManager fm = FragmentActivity.getSupportFragmentManager();
fm.beginTransaction().remove(
fm.getBackStackEntryAt(fm.getBackStackEntryCount()-1););
the first yes
Can I handle the back button pressed manually to provide a different fragment than what is on the back stack?
Yes, you can, but you have to do it in your FragmentActivity or Activity so you will need some interface or your own logic around that, or
with your own logic implementing BackStackChangedListener, this is actually the interface i was talking about with logic, this listener gets notified anytime a Fragment goes or leaves the Backstack, hence the second yes
Third yes was for pampering.
Hope it helps Sir.
You cannot actually do this, but what you can do is, make these tansitions but hidden
go to fragment 3 from fragment 2
go to fragment 4 from 3.
By hidden I mean, that user does not see this happening (you can set the layout visibility as gone).
But this way, Fragment 2 will be in the stack.
To take out fragment 2 from the stack, do the following hidden operations:
go back to 1
go to 3 from 1
go to 4 from 3.
Hope this helps.

prevent android from killing fragment

I'm trying to create a fragment that exists for the entire duration of my app's lifecycle. I want it to be created only once and to be destroyed when the activity's onDestroy() function is called (so, ideally, never...). I understand that this goes against what android intended when it introduced fragments, but the nature of my project makes changing this requirement impossible.
What I would like clarification on, is this whole backstack business. I am slightly confused about what exactly the backstack represents, I understand that it is a stack of previous UI states, but does that mean it is a stack of the fragments that the user has currently iterated through? Or is this a stack of FragmentTransactions and when you popBackStack(), it "undo's" the last FragmentTransaction that was committed (and if so, what does it mean to "undo" a FragmentTransaction...does it just remove an added fragment and add a removed fragment, what if I want it to detach a fragment and not remove it when popBackStack() is called?)?
Finally, does calling detach() prevent Android from killing my Fragment (unlike remove() and replace() which will immediately call the onPause(), onStop(), onDestroy(), onDestroyView() sequence)? If so, how do I get popBackStack() to detach() my Fragment (can I even do this?)...should even be adding it to the backstack?
Further restraints on my project - I want all other fragments to behave normally (i.e. they should be able to be destroyed/created at will by the OS). This fragment will not open another fragment, but if I hit the back button, I want it to return to whatever fragment opened it (without destroying the fragment).
Any guidance and/or clarification on the issues I enumerated above would be greatly appreciated!! Thanks in advance!

Hide and Show Fragment?

I created fragment-A part of Activity-A, here I HIDE fragment-A and launch fragment-B, working fine. Now I detach fragment-B, so how Fragment-A come to know that now it's time to wakeup i.e. SHOW.
use addToBackStack method which is FragmentTransaction class. The transaction will be remembered and when you press the back button while Fragment B is active, Fragment A will be shown.
Override Fragment B's onDetach() method to unhide fragment A.
In case you need, this is a good guide on how to have fragments/activity interact with each other: http://developer.android.com/training/basics/fragments/communicating.html

Categories

Resources