I am using Fragments in a FragmentActivity with a FragmentManager.
I need to keep the last modifications in the fragment.
If I use FragmentTransaction.replace(); It will destroy the Fragment and reset it.
I decided to hide the previous Fragment and show the next one.
But the Fragment can be destroyed by an income call or a device rotation and they can't be shown anymore.
My question is how can I manage this?
If I use show(), hide() I need to forbid the fragment's destruction
If I use fragmentTransaction.replace() I need to save the last state.
The second solution looks better but I have no idea how to proceed.
Are there any other solutions?
Ok I fixed it.
THe trick is to use FragmentTransition.replace() and FragmentTransition.addTobackStack(null);
And overide onBackPressed of the activity like this:
#Override
public void onBackPressed()
{
}
Hope it will help!
Related
I have two Fragments in a TabLayout.
In one Fragment there is a method wich changes some values using the SharedPreferences.
When I change the values I always update the Textviews.
This works fine.
But when I go back into my first Fragment, I have the old values, which has been updated in the OnCreateView() method.
How I can check if a user goes back to the Fragment and updates the TextViews?
P.S. Sorry for my bad English
You can try to override the onResume method of the fragment and inside onResume re-read the values stored in Preferences or you can use a EventBus to propagate the changes or Use RxJava to change reactivily.
mViewPager.setOffscreenPageLimit(numberOfTabs);
This may help
I have v4.ViewPager inside Activity and use SlidingTabLayout from google's examples SlidingTabBasics. The problem I encounter is that each fragment retrieved from getItem(position) in v4.FragmentPagerAdapter has to refresh activity title. I have already learnt the hard way that FragmentPagerAdapter causes fragments to have really weird life callbacks so I can't probably use onResume or onStart. I noticed though that onCreateOptionsMenu(menu,inflater) gets called exactly when I want to refresh activity title. Is there a callback to supply actions when ViewPager has settled the fragment and it should change activity title?
Setting callback on ViewPager.onPageSelected(position) is inconvenient because I want this information to be propagated from fragment, not to fragment.
Currently I 'steal' onCreateOptionsMenu(menu,inflater) to do the work for me but it causes optimisation issues when no menu should be inflated but I still want the fragment to be able to affect activity title.
Have you tried this code in your fragment:
if(getActivity() != null){
getActivity().setTitle("new title");
}
Take into consideration that getActivity() will be null if the fragment is not yet attached to the activity.
Godspeed.
You can do this in different ways.
You can use interfaces in fragments that can be implemented by activity. But the drawback is, if you do have large number of fragments you must implement all of them.
Problem
onSaveInstanceState is being called on device rotation for a fragment which has been replaced in a fragment transaction. This is causing an IllegalStateException with the message "Content view not yet created.
Research
I have found the following two answers which seem most relevant
Using onSaveInstanceState with fragments in backstack?
onSaveInstanceState of previous fragment is called on-orientation of current fragment
What I've tried already
Following the answer to the second question I have removed addtobackstack from the fragment transaction but am still getting the error.
I read the accepted answer to the first question but am not sure how to get a fragment reference within onSaveInstanceState for the activity. I also don't particularly want to save that fragment, when the user leaves that fragment it's state does not need to be saved.
I have also tried adding if (getView() != null) {...} to the onSaveInstanceState in the fragment but this made no difference.
Questions
Why is onSaveInstanceState being called even though the fragment hasn't been added to the back stack?
Is the right approach to somehow kill the fragment when the user is done with it?
Is there a different solution?
Thank you in advance for your help.
Andrew
Edit
If I remove addtobackstack(null) from the transaction which adds the fragment and the one where it is removed the problem goes away, but then so does an important piece of functionality... I could add that case to my onBackPressed override function but it seems a bit of a hack, and not in a good way.
Adding an isVisible() check onSaveInstanceState of the fragment in the backstack should work.
No need to retain instance state when it is not visible.
So I have a fragment A which has a button to open another a fragment B. In fragment B I can pick some options, which is bundled into an Bundleobject. When I exit from fragment B, I want to refresh a TextView in fragment A.
Right now I'm using dismiss() method to remove the fragment, and then call back the fragment again so that onCreateView() is called. It works fine, but I don't want the animation where the fragment windows is run. So I like to not use dismiss() to remove the fragment instead I want to keep it on the Activity, but I need to know how I can refresh fragment A. I've tried overriding onActivityCreated() but it didn't result in the action I wanted.
So I wonder what's the approach if I want to refresh fragment A without having to dismiss it first so that onCreateView() can be called again.
I can attach code if needed. But maybe just an explanation is enough here?
you can use the life cycle function onResume() in fragment A to update the textview.
You can create your own Listener interface ( example how to do it or this) that listens when you remove your fragment, and you can get the event on Fragment A where you can setText to your TextView.
I have been asked an interview question: Can a fragment exist without activity? I searched for answers but didn't get a proper answer and explanation. Can someone help?
Yes, you can do this anywhere:
new YourFragment();
As fragments must have a parameter-less constructor.
However its lifecycle doesn't kick in until it is attached. So onAttach, onCreate, onCreateView, etc. are only called when it is attached. So most fragments do nothing until they are attached.
It can exist as an object in memory (by creating it with new), but it needs to be attached to an Activity in order to appear on the screen, assuming it has any UI (fragments don't have to have UI).
A Fragment can exist independently, but in order to display it, you need the help of an Activity. The Activity will act like a container for the Fragment(s).
A fragment is not required to be a part of the Activity layout; you may also use a fragment without its own UI as an invisible worker for the Activity but it needs to be attached to an Activity in order to appear on the screen.
As soon as you create an instance of the Fragment class, it exists, but in order for it to appear on the UI, you must attach that fragment to an activity because a fragment's lifecycle runs parallel to an activity's lifecycle. Without any call to Activity's onCreate(), there will be no call for onAttach(), onCreate(), onCreateView() and onActivityCreated() of fragment and so it can't be started.
I read above top rated answer , i am not disagreeing but android already provides to make independent fragment without activity DialogFragment , which extends fragment . if you want show in full screen first extends DialogFragment then
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light);
}
Android app must have an Activity or FragmentActivity that handles the fragment.
Fragment can't be initiated without Activity or FragmentActivity.