In order to focus on the problem I ended with the following test app which contains one activity and two fragments which are connected to FragmentStatePagerAdapter / ViewPager. I also have Runnable and Handler printing getView() every second (in production version it is used to show time in fragment's TextView).
When I start app, getView() shows maybe few nulls (I suppose due to fragment life cycle) and then I get some non nulls which is ok...
But if I suspend and wake up my device LogCat shows nulls all the time.
public class MainActivity extends SherlockFragmentActivity {
private SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
private Fragment myFragment1 = new MyFragment1();
private Fragment myFragment2 = new MyFragment2();
private Handler handler = new Handler();
private Runnable timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(sectionsPagerAdapter);
}
#Override
protected void onResume() {
super.onResume();
timer = new Runnable() {
public void run() {
System.out.println(myFragment1.getView()); // HERE getView gives null
handler.postDelayed(this, 1000); // refreshing every 1 sec
}
};
handler.removeCallbacks(timer);
handler.postDelayed(timer, 0);
}
#Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(timer);
}
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment;
if (position == 0) {
fragment = myFragment1;
} else {
fragment = myFragment2;
}
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Fragment 1";
case 1:
return "Fragment 2";
}
return null;
}
}
}
My fragment
public class MyFragment1 extends SherlockFragment {
private TextView textView2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((TextView) getView().findViewById(R.id.textView1)).setText("Fragment 1");
textView2 = (TextView) this.getView().findViewById(R.id.textView2);
}
public void updateTextView2(String text) {
textView2.setText(text);
}
Ok I solved my problem simply by adding:
if (myFragment1.getView() == null) {
viewPager.setAdapter(sectionsPagerAdapter);
}
I'm not happy about this solution but it works...
Related
My app have a fragmentActivity with tabBar which control over the view pager and contain 3 fragments ,when i update my class User in fragmentB,i need to display him in fragmentC.
my question is how to refresh fragmentC every time when i add new User.
before i wrote this question i tried all the solution from this questions:
1.Update ViewPager dynamically?
2.refresh fragment at reload
3.Update Fragment from ViewPager
4.ViewPager PagerAdapter not updating the View
5.How to update fragment content from activity (viewpager)?
here is my code
FragmentActivity:
public class MainActivityTab extends FragmentActivity {
public SectionsPagerAdapter mSectionsPagerAdapter;
public ViewPager mViewPager;
public static MainActivityTab instance = null;
public static MainActivityTab getInstance(){
return instance;
}
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_tab);
getWindow().setStatusBarColor(Color.rgb(191,76,12));
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(0).setIcon(R.drawable.icon_A);
tabLayout.getTabAt(1).setIcon(R.drawable.icon_B);
tabLayout.getTabAt(2).setIcon(R.drawable.icon_C);
mViewPager.setCurrentItem(1,false);
}
FragmentStatePagerAdapter:
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return FragmentA.newInstance();
case 1:
return FragmentA.newInstance();
case 2:
return FragmentC.newInstance();
default:
return null;
}
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return null;
}
}
FragmentB:
public class FragmentB extends Fragment {
EditText name;
EditText age;
Button btnSave;
public static FragmentB newInstance() {
FragmentB fragment = new FragmentB();
return fragment;
}
public FragmentB() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_new2, container, false);
name = (EditText) rootView.findViewById(R.id.name);
age = (EditText) rootView.findViewById(R.id.age);
btnSave = (Button) rootView.findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
UserManager.getInstance().creteUser(name.getText().toString(),age.getText().toString());
}
});
return rootView;
}
}
FragmentC:
public class FragmentC extends Fragment {
TextView nameTxt;
TextView ageTxt;
public static FragmentC newInstance(){
FragmentC instance = new FragmentC();
return instance;
}
public FragmentC(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_new3, container, false);
nameTxt = (TextView) rootView.findViewById(R.id.nameTxt);
ageTxt = (TextView) rootView.findViewById(R.id.ageTxt);
showUser();
return rootView;
}
public void showUser(){
if(UserManager.getInstance().getUser().getName()!=null){
nameTxt.setText(UserManager.getInstance().getUser().getName().toString());
ageTxt.setText(UserManager.getInstance().getUser().getAge().toString());
}
}
}
You could create a callback like:
public interface UserChangedCallback{
void onUserChanged();
}
and implement this callback on every view/fragment where you want to access the user:
#Override
public void onUserChanged() {
adapter.notifyDataSetChanged();
// or if you don't have an adapter refresh your textfields or whatever you want
}
Register the callback on the usermanager via UserManager.getInstance().registerCallback(this). Internally the usermanager have to add the callback to an internal list.
Create a private function inside your usermanager:
private void notifyCallbacks() {
for(UserChangedCallback callback : registeredCallbacks) {
callback.onUserChanged();
}
}
If you add/modify/delete a user you always have to call this function at the end. For example:
public void addUser(User user) {
users.add(user);
notifiCallbacks();
}
Now every view will be notified and refreshed.
Important!
Don't forget to unregister the callback from the usermanager on onDestroy() to avoid memory leaks.
Found solution:
i used in the method onPageSelected,when position is 2 my fragment detach and afterwards attach the fragment.
working perfectly!
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position==2){
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction().add(R.id.fragmant_new3, FragmentC.newInstance());
fragmentTransaction.detach(FragmentC.newInstance());
fragmentTransaction.attach(FragmentC.newInstance());
fragmentTransaction.commit();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
I am using view pager in activity and the sequence is - when I click the next button in first page, it should go to the second page. If I click the button in second page, it should go to third and so on. But my current app is behaving weird, i.e, When I click next button in second page, it goes to fourth page. Once again when I click first button in first page, it jumps to third page and so on..
Here is code.
public class ViewPagerActivity extends AppCompatActivity {
private ViewPager pager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_act);
setViewPager();
/*pager.setPageTransformer(true, new ZoomPageTransformer());
pager.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager()));
pager.post(new Runnable() {
#Override
public void run() {
pager.setCurrentItem(position); //don't know how to use this from fragment
}
});*/
}
private void setViewPager() {
pager = (ViewPager) findViewById(R.id.viewPager);
FragmentManager fm = getSupportFragmentManager();
ViewPagerAdapter adapter = new ViewPagerAdapter(fm);
pager.setAdapter(adapter);
}
public void setCurrentItem(int selectedPosition) {
pager.setCurrentItem(selectedPosition, true);
}
private class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return FirstPager.newInstance();
case 1:
return SecondPager.newInstance();
case 2:
return ThirdPager.newInstance();
case 3:
return FourthPager.newInstance();
case 4:
return FifthPager.newInstance();
case 5:
return SixthPager.newInstance();
default:
return null;
}
}
#Override
public int getCount() {
return 6;
}
}
FirstPager:
public class FirstPager extends Fragment {
private ImageView slideArrow;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_pager, container, false);
return v;
}
public static FirstPager newInstance() {
FirstPager fragment = new FirstPager ();
return fragment;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initUI();
}
private void initUI() {
slideArrow = (ImageView) getActivity().findViewById(R.id.next_arrow);
slideArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ViewPagerActivity)getActivity()).setCurrentItem (1);
}
});
}
}
Similarly in other fragments, I gave currentItem as 2,3,4,5 and 6. But it’s position behavior is not correct.
I referred some post but how in my case, how I will apply for a method to pass from fragment to make the position to work correctly?
I think the best approach would be this:
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
slideArrow = (ImageView) getActivity().findViewById(R.id.next_arrow);
Are you sure, you're calling this correctly? You're not.
You're calling findViewById of the arrow inside your activity. A ViewPager works by loading this page, and pre-loading next and previous pages for smooth swipe. And when you click the button on your 1st page, the last loaded page's (in your case 2nd page's) button click event gets called.
You must use the arrow button in each of your fragments
You can do something like this:
public class FirstPager extends Fragment {
private ImageView slideArrow;
View v;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.first_pager, container, false);
return v;
}
public static FirstPager newInstance() {
FirstPager fragment = new FirstPager ();
return fragment;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initUI();
}
private void initUI() {
slideArrow = (ImageView) v.findViewById(R.id.next_arrow); // Note the difference in this line.
slideArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ViewPagerActivity)getActivity()).setCurrentItem (1);
}
});
}
}
You can do two things here:
Firstly,Your viewpager has a override method onPageSelected() which return the select page positon. using this poistion element get the view and bind your button there and write listener there itself and write viewpager.setCurrentItem(i++); //if i is ur current position,but be sure to check if it is the last item.
secondly,you hold the position of the current fragment and
((ViewPagerActivity)getActivity()).setCurrentItem (next_frag_pos);
This code try working perfect.
public class ViewPagerActivity extends FragmentActivity {
private ViewPager pager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_act);
setViewPager();
/*pager.setPageTransformer(true, new ZoomPageTransformer());
pager.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager()));
pager.post(new Runnable() {
#Override
public void run() {
pager.setCurrentItem(position); //don't know how to use this from fragment
}
});*/
}
private void setViewPager() {
pager = (ViewPager) findViewById(R.id.viewPager);
FragmentManager fm = getSupportFragmentManager();
ViewPagerAdapter adapter = new ViewPagerAdapter(fm);
pager.setAdapter(adapter);
}
public void setCurrentItem(int selectedPosition) {
pager.setCurrentItem(selectedPosition, true);
}
private class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FirstPager();
case 1:
return new SecondPager();
case 2:
return new ThirdPager();
case 3:
return new FourthPager();
case 4:
return new FifthPager();
case 5:
return new SixthPager();
default:
return null;
}
}
#Override
public int getCount() {
return 6;
}
}
}
I have a Fragment with a TableLayout. The data in the table is from a SQLite db. The SQLite db is populated from a RESTful webservice in an AsyncTask in the MainActivity. The Fragment must wait for the task to complete before populating the table. The Fragment listens for the task onPostExecute() method to be called. When it is, the method onLoadAndStoreComplete in the Fragment is called. This all works.
What doesn't work is that I need the fragment activity in order to create new TableRows and TextViews in onLoadAndStoreComplete and I can't get it.
I've tried:
- making a class member fragmentActivity and assigning in onCreateView(), but by the time it gets to onLoadAndStoreComplete(), it is null.
- calling this.getActivity() again in onLoadAndStoreComplete(), but it returns null.
How do I get the fragment activity?
MyFragment.java:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
fragmentActivity = this.getActivity(); // return value is valid
...
}
#Override
public void onLoadAndStoreComplete() {
fragmentActivity = this.getActivity(); // returns null
...
}
from MainActivity.java (extends ActionBarActivity)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainActivity = this;
setContentView(R.layout.activity_main);
Resources resources = getResources();
String[] tabTitleArray = { resources.getString(R.string.first_fragment),
resources.getString(R.string.second_fragment),
resources.getString(R.string.help_fragment) };
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
mAdapter = new TabsPagerAdapter(this.getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabTitleArray) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
// On swiping the ViewPager, select the respective tab
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position) ;// on changing the page, make respected tab selected
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
String url = "http://myrestfulwebservice";
Fragment secondFragment = mAdapter.getItem(SECOND_TAB);
new LoadAndStoreDataTask((OnLoadAndStoreCompleteListener)secondFragment).execute(url);
}
} // end OnCreate
TabsPagerAdapter.java
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FirstFragment();
case 1:
return new SecondFragment();
case 2:
return new HelpFragment();
}
return null;
}
#Override
public int getCount() {
return 3; // get item count - equal to number of tabs
}
public static void setTabColor(TabHost tabhost) {
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) {
tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected
}
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
}
}
I haven't tested this, but it seems to me that it would work.
Add a Context parameter to the constructor for both TabsPagerAdapter and MyFragment.
Something like this:
TabsPagerAdapter.java:
public class TabsPagerAdapter extends FragmentPagerAdapter {
private Context mCtx;
public TabsPagerAdapter(FragmentManager fragmentManager, Context context) {
super(fragmentManager);
mCtx = context;
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FirstFragment(mCtx);
case 1:
return new SecondFragment(mCtx);
case 2:
return new HelpFragment(mCtx);
}
return null;
}
#Override
public int getCount() {
return 3; // get item count - equal to number of tabs
}
public static void setTabColor(TabHost tabhost) {
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) {
tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected
}
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
}
}
MainActivity.java:
mAdapter = new TabsPagerAdapter(this.getSupportFragmentManager(), this);
MyFragment.java (do this in FirstFragment, SecondFragment, and HelpFragment):
public static class MyFragment extends Fragment {
private Context fragmentActivity ;
public MyFragment(Context context){
fragmentActivity = context;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//.........
return rootView;
}
#Override
public void onLoadAndStoreComplete() {
//do something with fragmentActivity
}
}
It seems the AsyncTask starts too early. suggest loading the db in onActivityCreated(), it's be called after onCreateView
#Override
public void onActivityCreated (Bundle savedInstanceState) {
//load your db here
}
I am trying to create a very simple Android application that uses a FragmentPagerAdapter to swipe between three fragments. Each of the fragments contains a single EditText and has the exact same code.
The desired behavior is that when the user updates the EditText, the new value is saved to the application instance. Then, once a new fragment is selected, that new fragment should show the saved value. For some reason this is not working.
I also want the focused fragment to show the saved data when the application resumes (comes back from background). This too does not work.
I am really confused as to why something as simple as this is so difficult!
Here is my code so far:
StackOverflowDemoApplication.java:
public class StackOverflowDemoApplication extends Application {
private ApplicationData applicationData;
// the index of the last fragment that was displayed
private int lastItem = 0;
#Override
public void onCreate() {
applicationData = new ApplicationData();
}
public ApplicationData getApplicationData() {
return applicationData;
}
public int getLastItem() {
return lastItem;
}
public void setLastItem(int lastItem) {
this.lastItem = lastItem;
}
}
MainActivity.java
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
private static final String TAG = "MainActivity";
// the application instance
private StackOverflowDemoApplication application;
// the pager adapter
private SectionsPagerAdapter pagerAdapter;
// the view pager
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Save the application instance.
application = (StackOverflowDemoApplication) getApplication();
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(pagerAdapter);
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < pagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(pagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
pagerAdapter.notifyDataSetChanged();
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private int NUM_ITEMS = 3;
public SectionsPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public Fragment getItem(int position) {
// get the application data instance
ApplicationData data = application.getApplicationData();
switch (position) {
case 0:
return SecondFragment.newInstance(data);
case 1:
return FirstFragment.newInstance(data);
case 2:
return ThirdFragment.newInstance(data);
default:
return null;
}
}
#Override
public int getCount() {
return NUM_ITEMS;
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "FIRST FRAGMENT";
case 1:
return "SECOND FRAGMENT";
case 2:
return "THIRD FRAGMENT";
default:
return null;
}
}
}
#Override
public void onResume() {
// load the previous fragment
viewPager.setCurrentItem(application.getLastItem());
super.onResume();
}
#Override
public void onPause() {
// save the last fragment we used
application.setLastItem(viewPager.getCurrentItem());
super.onPause();
}
}
FirstFragment.java
public class FirstFragment extends Fragment {
private static final String TAG = "FirstFragment";
// the activity reference
private Activity activity;
// the application data
private ApplicationData data;
// the edit text
private EditText editText;
// are we currently loading data for this fragment?
private boolean loadingData = false;
public FirstFragment(ApplicationData data) {
super();
this.data = data;
}
public static FirstFragment newInstance(ApplicationData data) {
Log.e(TAG, "New instance called");
FirstFragment fragment = new FirstFragment(data);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e(TAG, "Creating FirstFragment view");
// inflate the view
View view = inflater.inflate(R.layout.first_fragment_layout, container, false);
// get the activity instance
activity = getActivity();
// the textview
editText = (EditText) view.findViewById(R.id.textView);
editText.addTextChangedListener(textWatcher);
// update the ui from the data
updateUIFromData();
return view;
}
public void updateUIFromData() {
// we have started loading the data
loadingData = true;
// if there is data
if (null != data) {
// set the value
if (null != data.getStringValue()) {
editText.setText(data.getStringValue());
}
}
// done loading the data
loadingData = false;
}
private void updateDataFromUi() {
data.setStringValue(editText.getText().toString());
}
private TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// if we are not loading data
if (!loadingData) {
// update the data from the ui
updateDataFromUi();
}
}
#Override
public void afterTextChanged(Editable s) {
}
};
}
Try to update view when fragment get visible to user as follows:
public class MyFragment extends Fragment
#Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (visible) {
**//Get new data and update views here**
}
}
Check this tutorial, it might help you:
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
You don't have to call notifyDataSetChanged() inside tabSelected callback.
You should update your fragment datas in onResumer() like TextView or EditText ... etc. See example:
Oncreate....
TextView mtextview = rootview.findViewById....
#Override
public void onResume() {
if(do some thing){
mtextview.setText("It did something");
}
super.onResume();
}
I make a FragmentPagerAdapter
There is a fragment which has a pager.
And the pager show 3 fragments and it works well.
But, After application cache clearing,
(It means... long time has passed application service backgrounded)
only pager showing up and 3 fragments have empty layout (white space).
As a result checking Log, I caught some problem lines...
After clearing cache, SectionsPagerAdapter() constructure called then
getItem() method never called.
Please Help me
public class ReviewFragment extends SherlockFragment {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.review_fragment, container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState == null) {
mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity().getSupportFragmentManager());
mViewPager = (ViewPager) getActivity().findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if (position == 0) {
return new BFragment();
} else {
return new CFragment();
}
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "1";
case 1:
return "2";
}
return null;
}
}