Android - Fragment onActivityResult avoid reloading - android

I'm using a Fragment that loads an Image into a ImageView.
I want to change that image with startActivityForResult() but when I finish that Activity and go back to the Fragment,the Fragment reloads the first image again.
Probably because of the onStart() method.
How can I avoid the Fragment to reload after onActivityResult()?

I found out what the problem was.
The fragment loads in the information in the onStart() method.
I had a look at the fragment lifecycle and found out that I had to
load the information in the onActivityCreated() method.
This way the view is already created and I can refer to my xml-objects.
Check out this image that explains the fragment lifecycle:
http://developer.android.com/images/activity_fragment_lifecycle.png

Related

Save Fragment Data onreplace and fill in after popbackstack

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

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.

How to retrieve last fragment after activity recreated?

I have an Activity which has a fragment. Inside the Fragment I am creating Camera Intent.
Since Camera is another activity, my main activity might be destroyed when user is taking picture.
After user finishes taking picture I want my activity come back to latest Fragment. Is there any way to do that?
I tried to use getSupportFragmentManager() but it didn't work. Since activity is being destroyed it didn't have any transaction info.
Thanks
There are lots of different ways to solve this issue. What I would do is store the latest fragment in a SingleTon class instance or in SharedPreferences and in onResume activity you can check the last fragment and attach it back your FragmentActivity.
The other solution would be to start your CameraActivity by expecting a result, e.g startActivityForResult. in that way your Fragment Activity will remain in the stack and whenever you are done with your CameraActivity, you will be returned back to onActivityResult of your MainActivity.
If you can share your code here, we can be more helpful.
Perhaps FragmentManager could work for you, launch the camera as a fragment and add the main fragment to the backstack.
http://developer.android.com/reference/android/app/FragmentManager.BackStackEntry.html
Have a look at the documentation.
For retrieving the past Fragment, add that Fragment to the back stack so that when the camera Activity is closed it will be again pushed to the front.
For example paste the following code in your Fragment
getActivity().getSupportFragmentManager().beginTransaction().addToBackStack("null").commit();

Understanding the fragment and activity lifecycle and backwards navigation

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

onLoadFinished not called on offscreen fragment until going back

I'm trying to implement List->Detail scheme with details pagination.
I've single Activity for ListView and different Activity with ViewPager.
In FragmentStatePagerAdapter.getItem i instatniate new fragment for page and pass item id via setArguments Bundle.
After opening pager Activity two Fragment pages are created and onCreate, onCreateView, onActivityCreated being called.
BUT
onLoadFinished is called only for first Fragment (currently visible).
If i go back onLoadFinished is called for second Fragment.
Strage thing is that when i swipe to second (onLoadFinished for third not called), back to first and again go forward to second OnLoadFinished is called for third fragment and every next - after going back and forward always next fragment is being fully created in advance.
Is this bug or feature?
How fully loading can be forced?
We've solved this by manually calling onStart and onResume in onCreate.. Then onLoadFinished is called

Categories

Resources