FragmentStatePagerAdapter not working with large amounts of Fragments










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;










share|improve this question



















  • 1





    How long does getDaysSinceEpoch() take? And how often is getCount() called? For each swipe?

    – 0X0nosugar
    Nov 13 '18 at 20:19







  • 1





    No, I asked for the duration of the execution

    – 0X0nosugar
    Nov 14 '18 at 5:05






  • 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.

    – 0X0nosugar
    Nov 14 '18 at 18:34







  • 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.

    – 0X0nosugar
    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















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;










share|improve this question



















  • 1





    How long does getDaysSinceEpoch() take? And how often is getCount() called? For each swipe?

    – 0X0nosugar
    Nov 13 '18 at 20:19







  • 1





    No, I asked for the duration of the execution

    – 0X0nosugar
    Nov 14 '18 at 5:05






  • 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.

    – 0X0nosugar
    Nov 14 '18 at 18:34







  • 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.

    – 0X0nosugar
    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













0












0








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;










share|improve this question
















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;







java android android-fragments android-viewpager fragmentstatepageradapter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 18:26







Jan Meyer

















asked Nov 13 '18 at 20:12









Jan MeyerJan Meyer

899




899







  • 1





    How long does getDaysSinceEpoch() take? And how often is getCount() called? For each swipe?

    – 0X0nosugar
    Nov 13 '18 at 20:19







  • 1





    No, I asked for the duration of the execution

    – 0X0nosugar
    Nov 14 '18 at 5:05






  • 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.

    – 0X0nosugar
    Nov 14 '18 at 18:34







  • 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.

    – 0X0nosugar
    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





    How long does getDaysSinceEpoch() take? And how often is getCount() called? For each swipe?

    – 0X0nosugar
    Nov 13 '18 at 20:19







  • 1





    No, I asked for the duration of the execution

    – 0X0nosugar
    Nov 14 '18 at 5:05






  • 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.

    – 0X0nosugar
    Nov 14 '18 at 18:34







  • 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.

    – 0X0nosugar
    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




1





How long does getDaysSinceEpoch() take? And how often is getCount() called? For each swipe?

– 0X0nosugar
Nov 13 '18 at 20:19






How long does getDaysSinceEpoch() take? And how often is getCount() called? For each swipe?

– 0X0nosugar
Nov 13 '18 at 20:19





1




1





No, I asked for the duration of the execution

– 0X0nosugar
Nov 14 '18 at 5:05





No, I asked for the duration of the execution

– 0X0nosugar
Nov 14 '18 at 5:05




1




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.

– 0X0nosugar
Nov 14 '18 at 18:34






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.

– 0X0nosugar
Nov 14 '18 at 18:34





1




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.

– 0X0nosugar
Nov 14 '18 at 18:58





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.

– 0X0nosugar
Nov 14 '18 at 18:58




1




1





Yep, the problem was the getDaysSinceEpoch() method. Now it's working fine. Thanks!

– Jan Meyer
Nov 15 '18 at 19:29





Yep, the problem was the getDaysSinceEpoch() method. Now it's working fine. Thanks!

– Jan Meyer
Nov 15 '18 at 19:29












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288782%2ffragmentstatepageradapter-not-working-with-large-amounts-of-fragments%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288782%2ffragmentstatepageradapter-not-working-with-large-amounts-of-fragments%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands