Does the fragment in it receive onDetach, onDestroy method? Or do I need to remove fragment first and then clear the view?
It doesn't matter the order you do it as far I understand it. If you clear the view, the fragment's elements will be removed from the activity's view but you still need to remove it from the fragment manager.
For example, I am creating something like the following in my activity:
ViewGroup viewFragments = (ViewGroup)findViewById(R.id.layout_fragments);
viewFragments.removeAllViews();
View child = ViewGroup.inflate(mActivity, R.layout.child_edit_item, null);
viewFragments.addView(child);
Fragment f = new MyFragment();
FragmentManager mgr = mActivity.getSupportFragmentManager();
FragmentTransaction trans = mgr.beginTransaction();
if (mgr.findFragmentByTag(p.getKey()) != null) {
trans.replace(R.id.fragment_container, f, p.getKey());
} else {
trans.add(R.id.fragment_container, f, p.getKey());
}
trans.commit();
Related
I use one activity multiple fragments in my app. I use a shared element animation. I have two fragments, one of them is a detail page.
I don't want to addToBackStack function for fragment transition. And the detail fragment returns without animation for some cases. (fragmentMain->fragmentDetail)
DetailFragment detailFragment = new DetailFragment();
Transition moveTransition = TransitionInflater.from(MainActivity.singleInstance)
.inflateTransition(android.R.transition.move); // android.R.transition.move
moveTransition.setDuration(400);
detailFragment.setSharedElementEnterTransition(moveTransition);
FragmentManager fragmentManager = MainActivity.getActivityFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()
.addSharedElement(sharedView, sharedView.getTransitionName())
.replace(R.id.mainLayout, detailFragment);
fragmentTransaction.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
This code works for me. I know that if I use addToBackStack, fragmentDetail->fragmentMain reveal animation works automatically. But I don't want to the use back stack.
Below code doesn't works for fragmentDetail->fragmentMain.
detailFragment.setSharedElementReturnTransition(moveTransition);
mainFragment.setSharedElementEnterTransition(moveTransition);
FragmentManager fragmentManager = MainActivity.getActivityFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()
.addSharedElement(textView, textView.getTransitionName())
.replace(R.id.mainLayout, mainFragment);
fragmentTransaction.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
How can I do shared element animation for this case?
Look at your onCreateView function. If It recreate the layout, it breaks the transition id.
Solution:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if(inflatedView != null)
return inflatedView;
inflatedView = inflater.inflate(R.layout.fragment_blank, container,
return inflatedView;
}
In my android activity I am using multiple fragments, I am successfully switching these fragments according to my requirement but problem is that when I am switching Fragment 1 to Fragment 2 and back from Fragment 2 to Fragment 1, Fragment 1 not showing previous data Fragment 1 start from stretch, but I want to show previous data same as I was selected.
Here is my code for go Fragment 1 (fragment_search_customerProfile) to Fragment 2 (fragment_CustomerInfo):
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.filterFram, new fragment_Search());
FrameLayout layout = (FrameLayout) rootView.findViewById(R.id.cpFrame);
layout.removeAllViewsInLayout();
transaction.remove(new fragment_search_customerProfile()).commit();
Here is my code for back Fragment 1 (fragment_search_customerProfile) fromFragment 2 (fragment_CustomerInfo):
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Log.d("fragment_Search", fromSearch + "");
transaction.replace(R.id.custIndoFram, new fragment_search_customerProfile());
FrameLayout layout = (FrameLayout) rootView.findViewById(R.id.custIndoFram);
layout.removeAllViewsInLayout();
transaction.remove(new fragment_CustomerInfo()).commit();
Can anyone explain me how can I stay save my fragment data?
Instead of transaction.replace(R.id.filterFram, new fragment_Search());
you can use transaction.add to add fragment while having the data of first one
transaction.add(R.id.filterFram, new fragment_Search());
transaction.addToBackStack(null);
From fragment two you can use .show instead of replace to show the first fragment
and hide fragment two from the first one. transaction.show(getSupportFragmentManager().findFragmentByTag("firstFragmentTag")).commit();
You can simple keep the instances of the fragments, so in your Activity you would have fields like that.
private Fragment fragment1;
private Fragment fragment2;
And now you can use the sexy replace() option,
To add Fragment1
if (fragment1 == null) {
fragment1 = new fragment_Search();
}
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.filterFram, fragment1);
and same for Fragment2
And you can hold the state of the fragment (data it retrieves) in the Fragment itself, and in your onViewCreated() you check, if the state is not null, you update the view immediately.
class Fragment1 extends Fragment {
private State state;
public void onViewCreated(View v) {
if (state != null) {
// Update UI here
}
}
}
UPDATE
Using your own code
private Fragment fragment1; // This is a field outside of below method
if (fragment1 == null) {
fragment1 = new fragment_Search();
}
FragmentManager manager = getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.filterFram, fragment1).commit();
FrameLayout layout = (FrameLayout) rootView.findViewById(R.id.cpFrame);
layout.removeAllViewsInLayout();
Going to fragment2 should be the same, and this code is used to go to Fragment1 and Fragment2, doesn't matter if you are going back or first time.
You have to add fragment instead of remove or replace
I am trying to add a fragment, then find a view inside said fragment, and add a view into it. However I keep getting a NullPointerException on this statement
FrameLayout container2 = (FrameLayout) fragment.getActivity().findViewById(R.id.content_frame);
Here is my code. Can someone tell me how to fix this please? thanks
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = new FragmentNavigationDrawer();
ViewGroup decor = (ViewGroup) getActivity().getWindow().getDecorView();
View child = decor.getChildAt(0);
decor.removeView(child);
fragmentTransaction.add(decor.getId(), fragment);
fragmentTransaction.commit();
FrameLayout container2 = (FrameLayout) fragment.getActivity().findViewById(R.id.content_frame);
container2.addView(child);
Just use a getter. Set a tag on your fragment so you can access it later, then either call getView() on your fragment to return its root view, or use a getter to access a specific View:
public class MainActivity extends AppCompatActivity {
//In onCreate
if (getFragmentManager().findFragmentByTag(FragmentNavigationDrawer.TAG) == null) {
getFragmentManager()
.beginTransaction()
.add(android.R.id.content, new FragmentNavigationDrawer(), FragmentNavigationDrawer.TAG)
.commit();
}
//Later, when you want to add said View:
FragmentNavigationDrawer frag =
(FragmentNavigationDrawer) getFragmentManager().findFragmentByTag(FragmentNavigationDrawer.TAG)
//Return the root view:
View fragRootView = frag.getView();
//Return a specific view:
frag.getUpdatableViewGroup().addView(newViewToAdd):
}
For your Fragment:
public class FragmentNavigationDrawer extends Fragment {
public static final String TAG = FragmentNavigationDrawer.class.getSimpleName();
FrameLayout updatableViewGroup;
//Can do this inside onCreateView() whilst inflating your Fragment's Views
//That's up to you.
#Override
public void onViewCreated (View view, Bundle savedInstanceState) {
updateableViewGroup = view.findViewById(R.id.updateable_view_group);
}
public FrameLayout getUpdatableViewGroup() {
return updateableViewGroup;
}
Be conscious of the Activity and Fragment life cycles however, and be careful not to attempt to access the Fragment's Views until they have finished inflating - onStart() of your Activity and later should be ok.
Please see the javadoc for FragmentTransaction.commit(). It says it will schedule a change to the fragment back stack. It doesn't happen immediately. It looks like you're expecting the fragment and its views to be instantly available.
Also, I'm really confused why you're reaching in a decor view to make changes. Usually you call out a view by id in the host activity's layout and make changes inside it.
I have a simple activity in which it's hosted a Navigation Drawer. One of its entry is a WebView, which takes a lot to loads (it displays Gmail, so that following all those nasty redirects takes quite a lot).
I would like to retain the fragment even when different selections occur. I thought that these solutions might work. They didn't.
1) Not re-creating the fragment: even if mWebmailFragment actually is not null, onCreateView() is called in any case.
private Fragment mWebmailFragment /*, the others */;
public void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case FRAGMENT_CODE_WEBMAIL:
mWebmailFragment = mWebmailFragment!=null ? mWebmailFragment : new WebmailFragment();
fragment = mWebmailFragment;
break;
// ...
}
2) if onCreateView() is called, retaining WebView object state may work. Well, it doesn't: debugging I discovered that savedInstanceState is always null. I don't know why.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
// Retain instance
setRetainInstance(true);
//....
// SavedState
if (savedInstanceState != null) {
mWebView.restoreState(savedInstanceState);
} else {
// do stuff
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
I think you haven't use hide/Show properly. In your fragment transaction use add() to load the fragment instead of replace().
Change this
final FragmentManager myFragmentManger = myContext
.getSupportFragmentManager();
final FragmentTransaction myFragmentTransaction = myFragmentManger
.beginTransaction();
myFragmentTransaction.replace(R.id.content, aFragment,
aFragmentTag.getFragment());
to
final FragmentManager myFragmentManger = myContext
.getSupportFragmentManager();
final FragmentTransaction myFragmentTransaction = myFragmentManger
.beginTransaction();
myFragmentTransaction.add(R.id.content, aFragment,
aFragmentTag.getFragment());
If you use replace, createview() of fragment will be called everytime. Try to use the add() as above code does.
I am creating a Fragment FragParent displaying several input-fields, one of which is a sub-Fragment frag, with specialized audio controls.
The layouts of both fragments are loaded from XML in their onCreateView() methods.
Code I am using is below, but I have some questions about it:
A) is it OK to replace() the sub-Fragment if it is already returned by findFragmentByTag(), or is that an unnecessary step?
B) can the sub-Fragment be instantiated and replaced in the layout, before the parent Fragment completes the inflate() call?
<!-- language: lang-java -->
// Load sub-Fragment with audio UI.
String fragStr = "fragment_audio_str";
int fragView = R.id.frag_audio_frame_layout;
FragmentManager fm = getChildFragmentManager();
FragmentTransaction fmt = fm.beginTransaction();
Fragment frag = (Fragment) fm.findFragmentByTag(fragStr);
if (null == frag) {
frag = new Fragment();
}
fmt.replace(fragView, frag, fragStr);
fmt.commit();
return inflater.inflate(R.layout.fragment_parent, container, false);
} // END onCreateView() of FragParent
A) is it OK to replace() the sub-Fragment if it is already returned by findFragmentByTag(), or is that an unnecessary step?
No, it shouldn't be necessary to replace the fragment if it is already there. However you need to replace your child fragment if it isn't there.
B) can the sub-Fragment be instantiated and replaced in the layout, before the parent Fragment completes the inflate() call?
No, you need to inflate the parent Fragment layout and then add your child Fragment to it before returning the parent's View.
Try something like this:
View parentFragLayout = inflater.inflate(R.layout.fragment_parent, container, false);
FrameLayout childFragContainer = (FrameLayout) parentFragLayout.findViewById(R.id.frag_audio_frame_layout);
// Load sub-Fragment with audio UI.
String fragStr = "fragment_audio_str";
FragmentManager fm = getChildFragmentManager();
Fragment frag = fm.findFragmentByTag(fragStr);
// Child fragment isn't there, so add it
if (frag == null) {
frag = new Fragment();
FragmentTransaction fmt = fm.beginTransaction();
fmt.replace(childFragContainer.getId(), frag, fragStr);
fmt.commit();
}
return parentFragLayout;
Unless your Fragment is contained your XML layout (which, since you're adding it in the onCreateView, I'm assuming that it is not contained in your XML layout) you will still need to use FragmentManager to add the Fragment. However, you should use add() instead of replace(). You should only call replace() when you are actually replacing another Fragment.
While I'm not a hundred percent sure about the second question, I'm pretty sure that it's a no.