How to retrieve last fragment after activity recreated? - android

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();

Related

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.

How to share fragments through activities?

I need a way to share Fragments through different activities, so for example I have MainActivity with a Fragment and when I go to SecondActivity I need that same Fragment loaded, but the Fragment can vary so it would not always be the same one.
I've guessed that I could get the actual Fragments id or tag and pass it on the Intent so I could retrieve it on SecondActivity and use it to load the correct Fragment, but I don't know how.
You can't. You have to create a new instance for the fragment and load again the data in it. If you created the fragment in a good way, it is a really simple task. The reasons why you can't reuse the fragment are the following:
If you could do something like that, what would happend to this fragment's lifecycle? What about going back from current activity to the other? Everything can be messed up.
Every activity has its own FragmentManager and they are never shared between activities, so you can't ask in another's activity for a fragment that doesn't belongs to it.
If you have some doubts on how to pass data using intents between activities to tell the fragment what to load, have a look at this post.

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

Android - Fragment onActivityResult avoid reloading

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

Reload the start activity?

Pseudocode :
The initial Activity starts. (ActivityStartScreen)
A button is pressed that starts a new Activity (ActivityOtherScreen) by using an Intent.
The new Activity has a button that loads the initial Activity.
My question is, is the original activity gone once it 'loses focus' (when the 1st button is pressed), or is it stored somewhere, and is there a way to retrieve it?
Currently, Im re-creating the original activity with an intent. I bet this isnt the proper way.
The previous Activities are stored in the Activity stack, to return to previous activity, just call finish() on current activity. Note that this way you lose the 2nd activity.
You simply must call the finish() method from your second activity to go back with the states all being the same.
Here is a helpful warriorpoint blog post tutorial that will walk you through it.

Categories

Resources