Popping Fragment from backstack on BACK key press - android

I am adding a Fragment and transaction is committed to the back stack using the following code:
getSupportFragmentManager().beginTransaction().add(id, fragment, "TAG").addToBackStack(null).commit();
Now when is press the BACK key. Activity is getting finished. But I just want to remove the fragment.
Should I manually handle the BACK key press and do the task of popping backstack or is there any way I can make activity to handle this automatically.

I found one reason for why it may happen:
If you are using app.Activity but making transactions with SupportFragmentManager, or using support.Activity and making transactions with FragmentManager, it seems that the activity ignores the fragments back-stack.
The usage should be consistent, support or normal.

Related

Android backstack fragments and data saving

I've been struggling with this problem for like a week, it's basically a Backstack problem I didn't found an optimal solution yet. I am working on a project which's flow is like this:
Activity (Fragment_A -> Fragment_B -> Fragment_C -> Fragment_A)
I am setting on Fragment_A one DatePicker and a TimePicker and I have 2 TextViews that redirect the user to Fragment_B. From Fragment_B I send some data to Fragment_C that converts the data and sends it back to Fragment_A.
The problem is that as far as I've understood is that when going back to Fragment_A it goes with a new instance, not the old, so my data there is lost. I resolved for now with sharedPreferences but it seems too complicated for a solution. I've tried with custom beginTransaction() and Navigation component but it didn't turn out well.
I was close, and when going from Fragment_C to Fragment_A there was the new instance on top of the old and at onBackPressed it popped the new instance, and showed me the old but how can I resolve so I won't need to press the back button? I tried to send a Boolean so if it's a new instance pop it, but it popped the old one I guess, because when I pressed back, it just quit the Activity instead of popping the new one.
Any help is appreciated!
An Activity-scoped ViewModel will make sharing data between child Fragments easier. Your Fragments observe the data in the ViewModel, so you don't even have to worry about passing data around. And with Navigation Component, you can use popUpTo and popUpToInclusive to pop Fragments B and C off the back stack, leaving just Fragment A.
If you're not familiar with these concepts, this free course covers them all wonderfully:
https://www.udacity.com/course/developing-android-apps-with-kotlin--ud9012

Make FragmentTransaction From Activity Started From Fragment

So right now I have a situation in which I have three fragments are committed in such an order:
Fragment A -> Fragment B -> Fragment C
Then, I start an Activity from Fragment C. The issue arises when I want to pop the back stack so the user is brought to Fragment B after the activity finishes. If I attempt to pop the back stack from the Activity before calling finish(), I get an IllegalStateException, saying that the action cannot be performed after onSaveInstanceState. Thus, is it even possible to make changes to the FragmentManager responsible for the fragments from the Activity?
How does this sound myrocks2? Android: how to make an activity return results to the activity which calls it?
First activity can start a second activity and expect a result. Upon getting back a result it knows second activity did its job, and now it's required to remove fragment c. (I don't know the logic of your app, but that can work)
Someone who thinks he is so smart gave you a negative vote, but I made sure to go away. There are no dumb questions.

Handle back pressed of multiple fragments in android

Hi Currently am developing application using fragments.Totally i have 15 fragments and loaded it in single Activity.While onBack Pressing on Each fragment will launch previous fragment.I google about it and also i got answers for it.but i just want to know which is more efficient way handle this.
By calling addToBackStack(), the replace transaction is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button. Reference
If you add multiple changes to the transaction (such as another add() or remove()) and call addToBackStack(), then all changes applied before you call commit() are added to the back stack as a single transaction and the Back button will reverse them all together.
Just add this transaction.addToBackStack(null);
For more information you may visit Handling back button press Inside Fragments

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!

Single activity Fragment based app and back button

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.

Categories

Resources