Why on calling first fragment second fragment is automatically getting called? - android

I am using ViewPager to show 4 fragments but the problem is that on loading first fragment second fragment is also getting called i.e. the onActivityCreate() method of both the fragments is getting called together and on swiping to second fragment third fragment's onActivityCreate() and so on for other fragments and for last fragment no onActivityCreate() is getting called. How to resolve that.
Here are my files:
Purchase Details
public class PurchaseDetails extends AppCompatActivity implements ActionBar.TabListener {
ViewPager viewPager;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_purchase_details);
viewPager=findViewById(R.id.master_purchase_pager);
FragmentManager fm=getSupportFragmentManager();
actionBar = getSupportActionBar();
actionBar.setTitle("Purchase Details");
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
viewPager.setAdapter(new PurchaseFragmentAdapter(fm));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("Date Selector");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("Week");
tab2.setTabListener(this);
ActionBar.Tab tab3 = actionBar.newTab();
tab3.setText("Month");
tab3.setTabListener(this);
ActionBar.Tab tab4 = actionBar.newTab();
tab4.setText("Year");
tab4.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
actionBar.addTab(tab4);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
PurchaseFragmentAdapter
public class PurchaseFragmentAdapter extends FragmentPagerAdapter {
public PurchaseFragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment=null;
if(position==0)
{
fragment= new PurchaseDateSelector();
}
else if(position==1)
{
fragment= new PurchaseWeek();
}
else if(position==2)
{
fragment= new PurchaseMonth();
}
else if(position==3)
{
fragment= new PurchaseYear();
}
return fragment;
}
#Override
public int getCount() {
return 4;
}
}
PurchaseDateSelector
public class PurchaseDateSelector extends Fragment
{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_date_selector_fragment,container,false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Purchase Date Selector", Toast.LENGTH_SHORT).show();
}
}
PurchaseWeek
public class PurchaseWeek extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_week_fragment,container,false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Purchase Week", Toast.LENGTH_SHORT).show();
}
}
PurchaseMonth
public class PurchaseMonth extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_month_fragment,container,false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Purchase Month", Toast.LENGTH_SHORT).show();
}
}
PurchaseYear
public class PurchaseYear extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_year_fragment,container,false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Purchase Year", Toast.LENGTH_SHORT).show();
}
}

The ViewPager doesn’t create all its pages at once. When using a lot of pages this would be horribly slow and even unnecessary if the user would never swipe through all these pages. By default the ViewPager only creates the current page as well as the off-screen pages to the left and right of the current page.
So what is solution of this problem, Is their any?
yeah there is,
mViewPager.setOffscreenPageLimit(int limit);
Parameters : limit – How many pages will be kept offscreen in an idle
state.
ViewPager require a minimum of 1 offscreen pages
If you set value to 0 then you should be getting a warning about this in LogCat, something like:
Requested offscreen page limit 0 too small;
defaulting to1
Its because ViewPager is based on concept of keep ready next page to be loaded. ViewPager can not the page which not exist still.
Thanks to Tech Code Geek

This is how ViewPager has been designed to work. By default the ViewPager only creates the current page as well as the off-screen pages to the left and right of the current page. This is to allow smooth transition between the pages.
You can also set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.
Read More

That's the default and expected behavior of view pager, it creates each fragment to its left and right along with the visible fragment for smooth animations, you can't restrict fragment from creation, may be what you can do is restrict the loading content on non-visible fragments using setuserVisibleHint() method
#Override
public void setUserVisibleHint(boolean visible){
if(visible){
//load content
}
}

Related

Reload Nested Fragments inside Fragment using ViewPager

i have an activity with fragments and inside one fragment i nested 3 other fragments and i use viewpager to navigate between them, but when i want to change this "main fragment" which has the nested fragments i call transaction.replace and it changes the fragment but when i come back at this fragment the nested fragments are not shown again, how can i reload them everytime i launch my fragment ?
i have tried to implement them inside my xml layout but it doesn't seems to work, i need help please !
FragmentWithViewPager:
public class FragmentWithViewPager extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_fragwithviewpager, null);
mActionBar = getActivity().getActionBar();
mActionBar.show();
mActionBar.setTitle("MyFragment");
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mPager = (ViewPager) view
.findViewById(R.id.MyFragmentMainPager);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());
tab.setIcon(tabIcon_active[tab.getPosition()]);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// /tab.setIcon(null);
tab.setIcon(tabIcon[tab.getPosition()]);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
tab.setIcon(tabIcon_active[tab.getPosition()]);
}
};
if(mActionBar.getTabCount() < 3){
for (int i=0; i < tabIcon.length; i++)
{
mActionBar.addTab(mActionBar.newTab()
.setIcon(tabIcon[i])
.setTabListener(tabListener));
}
}
mPagerAdapter = new MyFragmentsMainPagerAdapter(getFragmentManager());
new setAdapterTask().execute();
return view;
}
rivate class setAdapterTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void result) {
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
});
}
}
The Fragment that has the 3 other Fragments nested with the Viewpager should use the ChildFragmentManager.
In your activity you have your main_fragment_container in the xml that you replace or add your fragments. Correct? If true then inside the Fragment that has the Viewpager all the fragment transactions are automatically done for you if you used a FragmentPagerAdapter to accompany the Viewpager.
Then If you replace or add fragments in your main_fragment_container should not affect the contents of the Viewpager at all. When you reload the fragment with the viewpager the viewpager will reload as well if you have passed it the ChildFragmentManager.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_parent, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
mViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 4;
}
#Override
public Fragment getItem(int position) {
Bundle args = new Bundle();
args.putInt(TextViewFragment.POSITION_KEY, position);
return TextViewFragment.newInstance(args);
}
}
hide(MainFragment)
add(NewFragment)->
remove or hide(NewFragment)
show(MainFragment).
getSupportFragmentManager().beginTransition().hide(MainFragment).commit().
getSupportFragmentManager().beginTransition().add(R.id.containerid,MainFragment).commit().

Android ActionBar TabNavigation switching between tabs clears View

I'm trying to convert my app to TabNavigation layout using this tutorial. First and last tab layout are always the same(just content changes), only second is different in every app. So I want from second tab manage others, e.g. in first one is imageView and from second tab I can firstTab.imageView.setImageResources(/somePic) to display some picture. It works fine but I noticed that app starts on tab1 and show picture, then I switch to tab2 and back to tab1, it's still there, but when I go to tab3->tab1 or tab3->tab2->tab1 the pictire is gone, so I have to go tab2->tab1
again to set the picture. Why is it happening and how to avoid that? I'm a begginer in android so thanks a lot for any help.
TabActivity:
public class TabActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager) findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabReselected(android.app.ActionBar.Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Podgląd").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Dane").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Rozwiązanie").setTabListener(tabListener));
}
}
TabPagerAdapter:
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
//Fragement for Android Tab
return new Review();
case 1:
//Fragment for Ios Tab
return new Data();
case 2:
//Fragment for Windows Tab
return new Solution();
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3; //No of Tabs
}
}
Data class(second tab)
public class Data extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View ios = inflater.inflate(R.layout.ios_frag, container, false);
((TextView)ios.findViewById(R.id.textView)).setText("iOS");
Review.imageView.setImageResource(R.drawable.ic_launcher);
return ios;
}}
Review class(first tab)
public class Review extends Fragment {
public static ImageView imageView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.android_frag, container, false);
//((TextView)android.findViewById(R.id.textView)).setText("Android");
imageView = (ImageView)android.findViewById(R.id.imageView);
return android;
}
}
Solution class(third tab)
public class Solution extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View windows = inflater.inflate(R.layout.windows_frag, container, false);
((TextView)windows.findViewById(R.id.textView)).setText("Windows");
return windows;
}}
I have read now about sending data with Bundle cause it saves app state but it doesn't work, I think it's because tab classes doesn't extends Activity, am I wrong?

Android replace Fragments

I want to replace a Fragment by a new Fragment when an onClick method is called. I wanted to do it this way:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.remove(this);
transaction.addToBackStack(null);
transaction.add(new ChooseCocktailFragment(), null);
transaction.commit();
The only thing that happens is the current Fragment is removed and the new one doesn't show. What am I doing wrong here?
Also, I use a ViewPager and this is the first time for me working with fragments. Usually I would have a Container with its ID but here the ViewPager contains the fragments, therefore I don't know how to use the transaction.replace(...) method.
You should use a FragmentPagerAdapter which is adequate to put Fragments inside ViewPagers.
A complete tutorial from android developer here
EDIT:
First of all, you need to override the getItemPosition of the adapter, and return the POSITION_NONE constant in order to be able to replace pager's fragments.
#Override
public int getItemPosition(Object object)
{
return POSITION_NONE;
}
Then create a listner for handling fragment switching:
public interface MyListener
{
public void onFragmentChange();
}
Your adapter should implements the listner and add some code inside onFragmentChange() method:
public class TabsPagerAdapter extends FragmentPagerAdapter {
private FragmentManager fm;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
this.fm = fm;
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
fragment1 = TopRatedFragment.newInstance(this);
return fragment1;
case 1:
// ...
return fragment2;
case 2:
// ...
return fragment3;
case 3:
// ...
return fragment4;
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 4;
}
public void onFragmentChange(){
fm.beginTransaction().remove(fragment1).commit();
fragment1 = NewFragment.newInstance();
notifyDataSetChanged(); // notify the viewpager adapter that content has changed
}
}
Pass an instance of the listner in the fragment you want to remove/replace later :
public class TopRatedFragment extends Fragment {
public static TopRatedFragment newInstance(MyListner listner) {
this.myListner = listner;
return new TopRatedFragment ();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// some logical here ... and when you want to change fragment do this
myListner.onFragmentChange();
}
}
Here is some more explanation of this.
You should also read about FragmentStatePagerAdapter : here
Hope it helps !
You forget hide your fragment after another fragment is added up on your fragment.
Just add more a statement transaction.hide(Your_Fragment) before transaction.commit()
#mansoulx
Here is my code which my answer refers to. Since i implemented it using a tutorial it is very similiar, just Tab names and the content of the tabs changed. I also have 4 tabs, not three - but the problem would be the same:
TabsPagerAdapter.java
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
MainActivity.java
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Tab1", "Tab2", "Tab3" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
let's say all Fragments would look like this:
Fragment1.java
public class TopRatedFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
return rootView;
}
}
The thing which should be done now is:
Replace Fragment1 on Tab1 with FragmentNew which should appear in Tab1 too! The OnClick-Event takes place in the fragment1.java for example...

where can I write the java code in fragments?

iam a beginner in android
i wanted to make tabs
i took the sample and edited it to make a layout for every tab
but i couldn't find where to put the listeners of buttons of each layout of each tab in the code !
public class Game extends FragmentActivity implements ActionBar.TabListener {
static int Tab_no ;
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// For each of the sections in the app, add a tab to the action bar.
actionBar.addTab(actionBar.newTab().setTag("round").setText("Round").setTabListener(this));
actionBar.addTab(actionBar.newTab().setTag("score").setText("Score").setTabListener(this));
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) { // 3ashan law rege3na men ay makan yrg3ny le mkany
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getActionBar().setSelectedNavigationItem(
savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
#Override
public void onSaveInstanceState(Bundle outState) { // 3ashan law killed yrg3ny tany le makany
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM,
getActionBar().getSelectedNavigationIndex());
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, show the tab contents in the container
Fragment fragment = new SectionFragment();
Tab_no = tab.getPosition() + 1 ;
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A dummy fragment representing a section of the app, but that simply displays dummy text.
*/
public static class SectionFragment extends Fragment {
public SectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (Tab_no==1)
{
return inflater.inflate(R.layout.round , container, false);
}
else {
return inflater.inflate(R.layout.score , container, false);
}
}
}
}
and sorry for my bad english ! :)
You can write within onCreateView()
public class fragmentname extends Fragment{
Activity referenceActivity;
View parentHolder;
Button backBtn;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
referenceActivity = getActivity();
parentHolder = inflater.inflate(R.layout.yourlayout, container,
false);
backBtn = (Button) parentHolder.findViewById(R.id.back_btn);
backBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
return parentHolder;
}
}

ActionBarSherlock with ViewPager not calling ViewPager fragments lifecycle methods when going to and returning from backstack

I have an application that uses ActionBarSherlock and inside the main fragment I have a ViewPager which uses several fragments to display different objects of a list.
Main Fragment:
public class CollectionDemoFragment extends SherlockFragment {
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
ViewPager mViewPager;
public CollectionDemoFragment() {
setTitle(R.string.title);
setHasOptionsMenu(true);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.demo_fragment, container, false);
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
return view;
}
#Override
public void onPause() {
//This runs when the fragment goes to backstack
super.onPause();
}
#Override
public void onResume() {
//This runs when the fragment returns from backstack
super.onResume();
}
}
ViewPagerAdapter:
public class DemoCollectionPagerAdapter extends
FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = new DemoObjectFragment();
Bundle args = new Bundle();
args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public int getCount() {
return 100;
}
#Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position + 1);
}
}
Inside each of this fragments I can create a new Main fragment with a new list to display in the ViewPager and replace the content with this new fragment.
ViewPager Fragments:
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(
Integer.toString(args.getInt(ARG_OBJECT)));
//Setup components
return rootView;
}
#Override
public void setMenuVisibility(final boolean visible) {
if (visible) {
//Do something
}
super.setMenuVisibility(visible);
}
#Override
public void onPause() {
//This should run when the fragment goes to backstack
super.onPause();
}
#Override
public void onResume() {
//This should run when the fragment returns from backstack
super.onResume();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.someComponent:
Fragment newContent = new CollectionDemoFragment();
switchContent(newContent, true);
break;
}
}
public void switchContent(Fragment newContent, boolean addToBackStack) {
if (newContent != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.replace(R.id.content_frame, newContent);
if (addToBackStack) {
ft.addToBackStack("" + newContent.getId());
}
ft.commit();
}
}
}
This works ok, until I press back and the previous main fragment returns from the backstack.
The screen is empty (since onCreateView is not called the layout is not inflated), the lifecycle methods from the fragments in the ViewPager are never called either when the main fragment enters the backstack, nor when it returns .
The only method called in the fragments of the ViewPager is the setMenuVisibility() so, only the code in there runs.
Anyone knows a way to fix this and why this happens?
Not sure if it matters but I have to support since android 2.3.
When you are creating the view adapter You have to pass the fragment child manager as argument - getChildFragmentManager() instead of getFragmentManager().
Instead of,
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getFragmentManager());
you should use,
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getChildFragmentManager());

Categories

Resources