Let me explain my application first.
I have an activity and fragments in it. I'm opening fragmentA onCreate of application. In fragmentA,before i open fragmentB by using support fragment manager's replace() method, i call a webService for some json datas.
Then i open fragmentB with these json datas. After i opened the fragmentB, i want to turn back to fragmentA by using device's default backbutton, and i don't want to handle backButtonPress(). Just go back to previous fragment. My app does this.
Here is the question. When i press back button, my fragmentA calls the service again and it affects the user to wait. But how can i just turn to the previous fragment ?
I am looking for the best solution to turn previous fragment.
Related
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.
I have FragmentX in a ViewPager. FragmentX has EditText's in it and a Button. The user presses the Button and FragmentY replaces FragmentX. The user then presses back and FragmentX has lost all of its input from the user.
How do you either:
a) Save the data in FragmentX before FragmentY appears then FragmentY is replaced by FragmentX retrieve the data and fill in the EditText's in FragmentX
(please don't reply with OnSaveInstanceState, as this does not work unless the Fragment is destroyed, which in this case it is not)
b) somehow keep the data in FragmentX so it is there when we go back to FragmentX from FragmentY..
Any suggestions?
Using addToBackStack() might help in your case.
If you return to a fragment from the back stack it does not re-create the fragment but re-uses the same instance and starts with onCreateView() in the fragment lifecycle, see Fragment lifecycle.
So if you want to store state you should use instance variables and not rely on onSaveInstanceState().
Check this out: Maintaining fragment states
I am now saving the data in FragmentX to SharedPreferences and overriding onBackPressed in the Activity, and have created a function in the Activity called popFromBackstack where the popBackStack() occurs.
In the functions in my Activity where i replace my Fragments i am now saving the data in FragmentX into SharedPrefs before the replace. I am also checking after the replace if the new Fragment is FragmentX and, if it is, i am filling the data into FragmentX from SharedPrefs.
I am also saving the data from FragmentX in onBackPressed in my Activity (if the current fragment is FragmentX), incase the user presses the back button.
I have also created a public static activity called popBackStack() in my Activity which i call from Fragments to pop the backstack. I am also saving the data from FragmentX here (if the current fragment being popped is FragmentX). Once the Fragment being popped is popped i am checking if the new Fragment is FragmentX, and filling in the data if it is...
Long winded approach but i couldn't figure out any other reliable way. This is working perfectly.
I suggest following guidelines that Google provides and implement an interface declared in your Fragment and save the Bundle or whatever object you want in the activity. Then, in your newInstace() static factory method pass that Bundle and recreate data as usual. Since you are using a ViewPager and it will always render the second fragment before the button is pushed (I assume your second fragment is in another tab) you still need to manage it via an interface. When the back button is pressed, the data will still be there, unless it is destroyed, and you still need to implement onSaveInstaceState() for that matter. You can also use setRetainInstance(boolean retain). See here for more details
I have an app that has one Activity that loads fragments… on initial startup it loads fragment A. From A the user can navigate to fragment B; and from B navigate to fragment C. Each time a fragment is replaced I do addToBackStack. The back button navigates as one would expect, C to B to A, and if you press back again the app exits.
I’m also using a Dropbox datastore to save all my app’s data so I can move seamlessly between devices and have all my data synced. This seems to be working well. As part of my data I store which fragment was last displayed. Now when my app starts on a second device it correctly displays the most recently opened fragment. This works fine, however, I no longer have a backstack. For example, if the app is showing fragment C device one and then I start the app on device two, as expected fragment C is shown on device two. But when I press the back button, the app exits instead of showing fragment B. (Which makes sense since on device two the app only loaded fragment C.)
So my question: How do I pre-populate my baskstack such that when pressing the back button on device two that it navigates to fragment B?
Check out TaskStackBuilder or the activity XML declaration android:parentActivityName.
I solved this by just committing multiple fragment transactions. Works for me
supportFragmentManager.beginTransaction().replace(R.id.content, Fragment1(), "f1").addToBackStack(null).commit()
supportFragmentManager.beginTransaction().replace(R.id.content, Fragment2(), "f2").addToBackStack(null).commit()
supportFragmentManager.beginTransaction().replace(R.id.content, Fragment3(), "f3").addToBackStack(null).commit()
supportFragmentManager.beginTransaction().replace(R.id.content, Fragment4(), "f4").commit()
However, for a cleaner and better solution you could also have a look at the Jetpack Navigation UI
I'm trying to understand some odd behavior. I have an ActivityA that calls a method in onCreate() to add FragmentA to R.id.fragment_container. Inside FragmentA I have a button that attaches FragmentB by using ActivityA's fragment manager (getActivity().getSupportFragmentManager()) and replacing the R.id.fragment_container and I also add it to the backstack. I also have another button that starts a new ActivityB.
When I navigate back from ActivityB I get: ActivityA onResume(), FragmentA onResume(). But when I navigate back from FragmentB I get: FragmentB onCreateView(), FragmentB onActivityCreated() then the 2 onResume().
So my questions is...why is the view state saved when a new activity is launched and not when the fragment is replaced and reattached. It looks much better to just restore that state rather than recreate the views and fetch that data again. This seems like opposite behavior from what I would expect so I'm clearly missing some fragment state saving/restoration step or something. It seems like the activity is just pausing FragmentA (and ActivityA) when ActivityB is launched and restoring it on back pressed but when FragmentB is attached FragmentA gets completely destroyed. I'm sure there's a way to prevent this I just can't seem to figure it out. Thoughts?
Just below your question are four tags android,android-fragments,android-lifecycle& android-navigation .Put your cursor over it for a while a black box will pop up . click on info tab and you will get best links to study that topics along with links to books .
Hope this will help you
A have a fragment (Fragment A) which gets a selected photo from a user. Once the user selects the photo in Fragment A I want to go to Fragment B and take the selected photo with me. I am also using viewpager so the user can swipe between the fragments...I want the user to be able to choose the photo and in the onActivityResult in Fragment A go to or start Fragment B. Is this possible?
You will need to use a listener (ie. FragmentA.onFragmentBListener) to communicate between fragments.
In Fragment B, you will trigger an action, and listen for it in FragmentA (which can then act on the event, by starting, or doing anything).
Here is a good answer:
Android: correct way of jumping between fragments