0

My Problem
I'm developing a little app to test ViewPager with FragmentStatePagerAdapter. The app displays a TextView. The content of the TextView changes for each page of the ViewPager. If I set the maximum amount of pages to a low number, it's working fine. But I need a page for each day since the beginning of the epoch. When I set getCount() to getDaysSinceEpoche() the app stops working properly. It takes up to a minute to change a page.

My question
What is causing this problem?
Maybe the FragmentStatePagerAdapter is not deleting the unused Fragments?

Edit
The getCount() method is getting called 16 times for each swipe. Why is this happening?

Adapter class

public class CustomViewPagerAdapter extends FragmentStatePagerAdapter{


public CustomViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {
    return FirstFragment.newInstance(i, "xyz");
}

@Override
public int getCount() {
    return getDaysSinceEpoch();
}

public int getDaysSinceEpoch() {

    Calendar now = Calendar.getInstance();
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(0); // start at EPOCH

    int days = 0;
    while (cal.getTimeInMillis() < now.getTimeInMillis()) {
        days += 1;
        cal.add(Calendar.DAY_OF_MONTH, 1); // increment one day at a time
    }
    return days;
}

Fragment class

public class FirstFragment extends android.support.v4.app.Fragment {

private String title;
private int page;

public static FirstFragment newInstance(int page, String title){
    FirstFragment firstFragment = new FirstFragment();
    Bundle args = new Bundle();
    args.putInt("int", page);
    args.putString("string", title);
    firstFragment.setArguments(args);
    return firstFragment;
}

//Store instance variables based oj the arguments passed

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    page = getArguments().getInt("int", 0);
    title = getArguments().getString("string" );
}

//Inflate the View for the fragment based on xml layout

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment, container, false);
    TextView tv = (TextView) view.findViewById(R.id.tv);
    tv.setText(page + " -- " + title);
    return view;
}
Jan Meyer
  • 214
  • 3
  • 10
  • 1
    How long does getDaysSinceEpoch() take? And how often is getCount() called? For each swipe? – Bö macht Blau Nov 13 '18 at 20:19
  • What do you mean by "How long does getDaysSinceEpoch() take?" Do you mean the amount of days it returns? – Jan Meyer Nov 13 '18 at 22:10
  • 1
    No, I asked for the duration of the execution – Bö macht Blau Nov 14 '18 at 05:05
  • What is the best way to measure execution time? – Jan Meyer Nov 14 '18 at 18:23
  • getCount() is called 16 times for each swipe and when starting the app 6 times. Why is this happening? – Jan Meyer Nov 14 '18 at 18:24
  • 1
    How to measure execution time: log the value of System.currentTimeMillis() at the start and the end of the method, the difference is the execution time in millisecs. I don't know what triggers getCount() (too lazy to read the source code right now) but I know it *is* called often.This means maybe you should not call getDaysSinceEpoch() from there. – Bö macht Blau Nov 14 '18 at 18:34
  • I guess it should be called only once per swipe, right? And why could calling getDaysSinceEpoch() from getCount() be a problem? It don’t see why this could be problematic – Jan Meyer Nov 14 '18 at 18:51
  • 1
    It can be a problem if it takes too long to execute (-> bad for performance). Even if it does not make your app slower, it is not necessary to have the value calculated more often than once per day. Doing things which are unnecessary means draining the battery for no reason at all. – Bö macht Blau Nov 14 '18 at 18:58
  • 1
    Yep, the problem was the getDaysSinceEpoch() method. Now it's working fine. Thanks! – Jan Meyer Nov 15 '18 at 19:29
  • 1
    I thought so. Happy coding :) – Bö macht Blau Nov 15 '18 at 19:33

0 Answers0