Please help me resolve my issue.
I have MainActivity
with a framelayout. I want to replace some fragments into the framelayout. Now I am encounering an issues, it's:
1) I created and put fragment A to framelayout. Fragment A called onCreateView
...etc.
2) Then I created and put fragment B to layout... Fragment A was put on backstack and it called onPause()
(not called onDeattach()
, onDestroy
...)
3) I pressed back button. Fragment A was got from backstack, but it called onCreateView()
again. This action make my app has some another issues.
So my question is how to store fragment A in backstack and it don't recreate view.
This's the method that was used to change fragment:
public static void setContent(FragmentManager managerFragment, Fragment detailFragment) {
if (managerFragment != null) {
if(lastFragment==null && detailFragment instanceof HomeVer3Fragment ||
(lastFragment!=null && detailFragment instanceof HomeVer3Fragment && lastFragment instanceof HomeVer3Fragment)){
return;
}
String tag=detailFragment.getClass().getName();
managerFragment.popBackStackImmediate(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction ft = managerFragment.beginTransaction();
ft.replace(R.id.content_frame, detailFragment);
ft.addToBackStack(tag);
ft.commit();
lastFragment = detailFragment;
}
}
Thank and sorry for my bad question, my english is not well.
onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.
onCreate() is called to do initial creation of the fragment. onCreateView() is called by Android once the Fragment should inflate a view. onViewCreated() is called after onCreateView() and ensures that the fragment's root view is non-null .
Calling addToBackStack() commits the transaction to the back stack. The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.
The onCreate() is called first, for doing any non-graphical initialisations. Next, you can assign and declare any View variables you want to use in onCreateView() . Afterwards, use onActivityCreated() to do any final initialisations you want to do once everything has completed.
check out my complete post over here Dynamically changing the fragments inside a fragment tab host?
As far your problem of reloading ( I assume ) you can do some thing like undermentioned:
(1) Initiate a Boolean value say
boolean android_hacker = false;
(2) Now say U need to fetch data and create some view using list view. Now at this stage set "android_hacker = true;" after you have fetched all data.
(3) Now when U again come back to same fragment say "FragmentA" then check value on OnCreateView as mentioned ..
if(android_hacker != true){
new GoAsyncTask().execute();
}else{
// Perform stuff U need
}
That's it. Hope it helps some one
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With