When to reference a fragment created in ViewPager - android

I have a ViewPager that sets up two fragments as swipeable tabs. I am trying to find these fragments in the hosting activity.
I set them up as class global variables:
private SharedBillFragment sharedBill;
private SeparateBillFragment separateBill;
and try to find them in the activity's onStart():
#Override
protected void onStart() {
super.onStart();
findFragments();
}
where findFragments() is (I know this is kind of a hack, but it should work for the moment...):
private void findFragments() {
sharedBill = (SharedBillFragment)
getFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewPager + ":" + 0);
separateBill = (SeparateBillFragment)
getFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewPager + ":" + 1);
}
But this is not working, the fragments are not found. When i call findFragments() in a method that is called later (following a user interaction), the fragments are found and I can use them. Example:
private void setCountryTip(String selectedCountry) {
findFragments();
sharedBill.setPercentage(getCountryTip(selectedCountry));
separateBill.setPercentage(getCountryTip(selectedCountry));
}
But even when called then, the fragments are only available in the method where findFragments() is called, even though I set them as class global variables.
Best case, I would like to find the fragments when the app is started and store them in class global variables. Any help how to achieve that?
EDIT:
Some more information on how my fragments are setup. In my MainActivity I have:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] mTabTitles = getResources().getStringArray(R.array.tabs);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mTabsAdapter = new TabsAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
mViewPager.setAdapter(mTabsAdapter);
for (String tab_name : mTabTitles) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
}
and my TabsAdapter:
public class TabsAdapter extends FragmentPagerAdapter {
final int NUMBER_OF_TABS = 2;
public TabsAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new SharedBillFragment();
case 1:
return new SeparateBillFragment();
}
return null;
}
#Override
public int getCount() {
return NUMBER_OF_TABS;
}
}
EDIT: Regarding the NPE, my spinner is setup as follows in the fragment:
private Spinner spCountry;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
spCountry = (Spinner) getActivity().findViewById(R.id.spCountry);
spCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
mCallback.onCountrySelected(spCountry.getItemAtPosition(pos).toString());
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public void setCountry(String country) {
ArrayAdapter spCountryAdapter = (ArrayAdapter) spCountry.getAdapter();
spCountry.setSelection(spCountryAdapter.getPosition(country));
}
The spinner gets its entries via the xml property "android:entries".

Not a very nice and re-usable solution, but since you are using static fragments it could work: make the adapters type Fragment, and initialize the fragments in the activity. Than add the two fragment to the adapter, and in the adapters getItem function only return the corresponding fragment.
Adapter:
public class TabsAdapter extends FragmentPagerAdapter {
List<Fragment> fragments;
public TabsAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int index) {
return fragments.get(index);
}
#Override
public int getCount() {
return fragments.size();
}
}
Activity:
sharedBillFragment = new SharedBillFragment();
separateBillFragment = new SeparateBillFragment();
List<Fragment> fragments = new ArrayList<Fragment>();
fragments.add(sharedBillFragment);
fragments.add(separateBillFragment);
mTabsAdapter = new TabsAdapter(getFragmentManager(), fragments);
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
mViewPager.setAdapter(mTabsAdapter);
Find the spinner in the onCreateView like this:
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, containter, false);
spCountry = (Spinner) getActivity().findViewById(R.id.spCountry);
...
return v;
}

Related

Android endless FragmentPagerAdapter using new adapter on swipe

I am trying to create an endless FragmentPagerAdapter , on swipe to replace the adapter with a new one, and depending on which side you swipe, a global variable to increase or decrease, so you can know on what position you are. So I have tried this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(0);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(1);
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
if (mViewPager.getCurrentItem() == 0) {
// left
mViewPager.setAdapter(null);
mViewPager.setAdapter(new SectionsPagerAdapter(getSupportFragmentManager()));
mViewPager.setCurrentItem(1);
MyVar.INST.position--;
} else if (mViewPager.getCurrentItem() == 2) {
// right
mViewPager.setAdapter(null);
mViewPager.setAdapter(new SectionsPagerAdapter(getSupportFragmentManager()));
mViewPager.setCurrentItem(1);
MyVar.INST.position++;
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
the problem is, even though I have set the offscreenPageLimit to 0, Android still caches the 3 fragment in the memory by default, so when I scroll to the right, the method inside OnPageChangeListener is not being called, therefore the global variable is not being incremented properly....
Any idea how can I fix that?
EDIT: Here is the adapter
private class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return Fragment1.newInstance(MyVar.INST.position + position);
}
#Override
public int getCount() {
return 3;
}
}
So I managed to do it like this, works good
private void setAdapter() {
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(1);
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
if (mViewPager.getCurrentItem() == 0) {
// left
mViewPager.removeAllViews();
//globalVariable--;
setAdapter();
} else if (mViewPager.getCurrentItem() == 2) {
// right
mViewPager.removeAllViews();
//globalVariable++;
setAdapter();
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
And the adapter stays the same
private class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return Fragment1.newInstance(MyVar.INST.position + position);
}
#Override
public int getCount() {
return 3;
}
The main thing that makes it work is the mViewPager.removeAllViews(); which removes the previous fragments, so it doesnt mess up.
Use the globalVariable defined in some other class, singleton for example, to know on which position you are from the start

Only first of two fragments in viewpager properly recreated on screen rotation

I have a ViewPager with 2 fragments setup as swipeable tabs. Everything works fine but when I rotate the screen, only the first fragment is properly recreated. In the second I get a NullPointerException as soon as a method is called that deals with a widget in the second fragment. I can work around this by setting setRetainInstance(true); on the second fragment. But shouldn't both fragments get recreated properly by the system?
My Activity is:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState !=null ) {
mPersons = savedInstanceState.getInt(STATE_PERSONS);
mPercentage = savedInstanceState.getInt(STATE_TIP);
}
// setup ActionBar
String[] mTabTitles = getResources().getStringArray(R.array.tabs);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// setup TabsAdapter and ViewPager
mTabsAdapter = new TabsAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
mViewPager.setAdapter(mTabsAdapter);
for (String tab_name : mTabTitles) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
...
My TabsAdapter is:
public class TabsAdapter extends FragmentPagerAdapter {
final int NUMBER_OF_TABS = 2;
public TabsAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new SharedBillFragment();
case 1:
return new SeparateBillFragment();
}
return null;
}
#Override
public int getCount() {
return NUMBER_OF_TABS;
}
}
In my fragments i initialize my widgets in onActivityCreate(), e.g. (I also tried to initialize them in onCreateView(), same result...:
private EditText etPersons;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
etPersons = (EditText) getActivity().findViewById(R.id.etPersonsS);
etPersons.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if (etPersons.length() > 0) {
mCallback.onPersonsSelected(Integer.parseInt(etPersons.getText().toString()));
} else {
mCallback.onPersonsSelected(0);
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});
...
}
E.g. the following method in the second fragment throws a NPE when called after a screen rotation:
public int getPersons() {
int persons = 0;
if (etPersons.length() > 0 ) {
persons = Integer.parseInt(etPersons.getText().toString());
}
return persons;
}
Specifically the line if (etPersons.length() > 0 ) {
You Should be using FragmentStatePagerAdapter instead of FragmentPagerAdapter. Only in FragmentStatePagerAdapter getItem() is called, hence recreated.

How to save state of Fragments in ViewPager when orientation changes

I have an application that Implements ActionBarSherlock, it's mainly composed of
ViewPager with different content of fragments (for each page), with ViewPager indicator.
Here is my onCreate Method of the Activity that extends SherlockFragmentActivity
// Assume that tabs,sources,types are string arrays e.g. tabs = ["tab1","tab2","tab3"]
// types = ["listview","listview","webview"]
// according to the type of each tab, the type of content of the fragment associated to that tab is determined (e.g. WebView or ListView)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
srcContext = getBaseContext();
srcActivity = SaudiActivity.this;
int selectPos = 0;
Intent sender = getIntent();
FragmentPagerAdapter mAdapter = new NewsListAdapter(
getSupportFragmentManager());
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(mAdapter);
TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(pager);
pager.setOffscreenPageLimit(tabs.length);
pager.setCurrentItem((tabs.length - 1));
currentPosition = (tabs.length - 1);
indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
currentPosition = position;
if ((type[position] == "listview" || type[position]
.equals("listview")) && loaded[position] == false) {
loaded[position] = true;
getNews(listViews[position], position);
}
}
#Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
getSupportActionBar().setCustomView(R.layout.logo);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
Context ctx = getSupportActionBar().getThemedContext();
SourcesAdapter adapter = new SourcesAdapter(ctx,
R.layout.navigation_list_item, src_name, icons, src_value);
}
Here are my News List Adapter (that creates the fragments),
public static class MyViewHolder {
public TextView title;
public ImageView icon;
}
class NewsListAdapter extends FragmentPagerAdapter {
public NewsListAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new NewsFragment(MyActivity.this, position);
}
// This is the number of pages -- 5
#Override
public int getCount() {
return tabs.length;
}
// This is the title of the page that will apppear on the "tab"
public CharSequence getPageTitle(int position) {
return tabs[position];
}
}
public List<NewsItem> NewsItems;
Finally here's my NewsFragment:
public static class NewsFragment extends Fragment {
private int position;
public NewsFragment() {
}
public NewsFragment(Context ctx, int pos) {
this.position = pos;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = null;
if (type[position].equals("listview")) {
// Put a ListView in the Fragment
} else {
// Put a WebView in the Fragment
}
return view;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
}
Let the fragment handle it's state by overriding onSaveInstanceState and placing the necessary values into outState. Recover that state in onCreateView using savedInstanceState. Also I'm pretty sure that if you're using Actionbarsherlock fragments need to extend SherlockFragment rather than Fragment.

How to update views of viewpager on swipe - android

I am using view-pager.In that I have three tabs : 1. History , 2. Main , 3. Map .
I have stopwatch into main activity.When I press the start and then after stop at that time value of the stopwatch add into the sqlite DB. And into history activity I am fetching the value of stopwatch from DB and store into the listview. It's working perfect.
But now I want the update the History activity when I swipe from the Main > History so After press the stop button,I got the last value into listview of history activity.
My question is how can I update the view on swipe the viewpager?
public class SwipeyTabsSampleActivity extends FragmentActivity {
private static final String [] TITLES = {
"HISTORY",
"MAIN",
"MAP"
};
private SwipeyTabs mTabs;
private ViewPager mViewPager;
//Handler handler=new Handler();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipeytab);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mTabs = (SwipeyTabs) findViewById(R.id.swipeytabs);
SwipeyTabsPagerAdapter adapter = new SwipeyTabsPagerAdapter(this,
getSupportFragmentManager());
mViewPager.setAdapter(adapter);
mTabs.setAdapter(adapter);
mViewPager.setOnPageChangeListener(mTabs);
mViewPager.setCurrentItem(1);
mViewPager.setOffscreenPageLimit(3);
}
private class SwipeyTabsPagerAdapter extends FragmentPagerAdapter implements
SwipeyTabsAdapter, ViewPager.OnPageChangeListener {
private final Context mContext;
ViewPager viewpager;
public SwipeyTabsPagerAdapter(Context context, FragmentManager fm, ViewPager mViewPager) {
super(fm);
this.mContext = context;
viewpager = mViewPager;
//viewpager.setAdapter(this);
viewpager.setOnPageChangeListener(this);
}
#Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
switch (position) {
case 0:
f = History.newInstance(position);
break;
case 1:
f = Main.newInstance(position);
break;
case 2:
f = Map.newInstance(position);
break;
}
return f;
}
#Override
public int getCount() {
return TITLES.length;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager pager = (ViewPager) container;
View view = (View) object;
pager.removeView(view);
}
public TextView getTab(final int position, SwipeyTabs root) {
TextView view = (TextView) LayoutInflater.from(mContext).inflate(
R.layout.swipey_tab_indicator, root, false);
view.setText(TITLES[position]);
view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mViewPager.setCurrentItem(position);
}
});
return view;
}
#Override
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int positiion) {
// TODO Auto-generated method stub
}
}
}
Add ViewPager OnPageChangeListener:
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
// your code here
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
This will affect the changes from one page to another page in view pager.
OnPageChangeListener pagechangelistener =new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
Logger.logMessage("Called first");
pageAdapter.notifyDataSetChanged();
indicator.setCurrentItem(arg0);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
Logger.logMessage("Called second");
}
#Override
public void onPageScrollStateChanged(int arg0) {
Logger.logMessage("Called third");
}
};
myViewPager.setOnPageChangeListener(pagechangelistener);
Use this in your page adapter.
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
Take one string and add the Getters and setters into your FragmentActivity class.
Like...
String HistoryFragment;
public String getHistoryFragment() {
return HistoryFragment;
}
public void setHistoryFragment(String historyFragment) {
HistoryFragment = historyFragment;
}
Then after we can access another fragment from our current fragment by calling findFragmentByTag(<tag of target fragment>).
For the more information check the link Communication between Fragments in ViewPager.
Have you tried implementing OnPageChanged`? You can capture when the user start swiping.
do pager.setOnPageChanged(new ....) and Eclipse will show you the methods you can override.
Then you can override onPageScrollStateChanged(int state)
onPageScrollStateChanged(int state){
if (state==OnPageChangeListener.SCROLL_STATE_DRAGGING){
// update here
}
}

getSupportFragmentManager() contains empty fragments after standby

After the device goes on standby, if I call getSupportFragmentManager() I get a FragmentManager that contains fragments not initialised with all their variables set to null.
Here there is some code:
this.mAdapter = new MyAdapter(getSupportFragmentManager());
this.mPager = (ViewPager) findViewById(R.id.pager);
this.mPager.setAdapter(mAdapter);
TabPageIndicator titleIndicator = (TabPageIndicator) findViewById(R.id.titles);
titleIndicator.setViewPager(mPager);
titleIndicator.setOnPageChangeListener(new OnPageChangeListener()
{
#Override
public void onPageSelected(int position)
{
mAdapter.getItem(position).onFocus();
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
#Override
public void onPageScrollStateChanged(int arg0)
{
}
});
This is MyAdapter class:
public static class MyAdapter extends FragmentStatePagerAdapter
{
ArrayList<ExtendedFragment> tabs = new ArrayList<ExtendedFragment>();
String[] titles = new String[] { "FRAG1", "FRAG2", "FRAG3", "FRAG4", "FRAG5", "FRAG6"};
public MyAdapter(FragmentManager fm)
{
super(fm);
tabs.add(new frag1());
tabs.add(new frag2());
tabs.add(new frag3());
tabs.add(new frag4());
tabs.add(new frag5());
tabs.add(new frag6());
}
#Override
public int getCount()
{
return tabs.size();
}
#Override
public ExtendedFragment getItem(int position)
{
return tabs.get(position);
}
#Override
public CharSequence getPageTitle(int position)
{
return titles[position];
}
}
The ExtendedFragment implements the method onFocus(), each fragment override that because i need to do some actions when a tab gets the focus. When I resume the device from standby and onFocus() is fired, if I try to get the View using getView() I get null.
Why my view is null?
You could try overriding onSaveInstanceState method.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mContentFragment = getSupportFragmentManager().getFragment(
savedInstanceState, "mContent");
}
if (mContentFragment == null) {
mContentFragment = new YourFragment();
}
}
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "mContent",
mContentFragment);
}

Categories

Resources