Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7949361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:58:36+00:00 2026-06-04T01:58:36+00:00

I have a problem with referencing my Fragments inside a ViewPager. I would like

  • 0

I have a problem with referencing my Fragments inside a ViewPager. I would like to do it because from my activity I’d like to refresh a fragment at a specified position (e.g. currently displayed fragment).

Currently I have something like this:

public static class MyPagerAdapter extends FragmentPagerAdapter {

    private static final String TAG = "MyPagerAdapter";
    private static HashMap<Integer, EventListFragment> mPageReferenceMap = new HashMap<Integer, EventListFragment>();

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

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

    @Override
    public Fragment getItem(int position) {
        Log.i(TAG, "getItem: "+position);
        int dateOffset = position-1;
        EventListFragment mFragment = EventListFragment.newInstance(dateOffset);
        mPageReferenceMap.put(position, mFragment);
        return mFragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        Log.i(TAG, "destroyItem: "+position);
        mPageReferenceMap.remove(position);
        super.destroyItem(container, position, object);
    }


    public EventListFragment getFragment(int key) {
        Log.i(TAG, "Size of pager references: "+mPageReferenceMap.size());
        return mPageReferenceMap.get(key);
    }
}

The problem is that the destroyItem() gets called more often than getItem(), so I’m left with null references. If I don’t use destroyItem() to clear references to destroyed fragments… well I reference fragments that don’t exist.

Is there any nice way to reference fragments that are created with EventListFragment mFragment = EventListFragment.newInstance(dateOffset);? Or what should I do to refresh a fragment inside a ViewPager from my activity (from options menu to be precise)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:58:38+00:00Added an answer on June 4, 2026 at 1:58 am

    I managed to solve it. The trick was to make a reference list inside Activity, not PagerAdapter. It goes like this:

    List<WeakReference<EventListFragment>> fragList = new ArrayList<WeakReference<EventListFragment>>();
    
    @Override
    public void onAttachFragment (Fragment fragment) {
        Log.i(TAG, "onAttachFragment: "+fragment);
        if(fragment.getClass()==EventListFragment.class){
            fragList.add(new WeakReference<EventListFragment>((EventListFragment)fragment));
        }
    }
    public EventListFragment getFragmentByPosition(int position) {
    
        EventListFragment ret = null;
        for(WeakReference<EventListFragment> ref : fragList) {
            EventListFragment f = ref.get();
            if(f != null) {
                if(f.getPosition()==position){
                    ret = f;
                }
            } else { //delete from list
                fragList.remove(f);
            }
        }
        return ret;
    
    }
    

    Of course your fragment has to implement a getPosition() function, but I needed something like this anyway, so it wasn’t a problem.

    Thanks Alex Lockwood for your suggestion with WeakReference!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my problem: I have an object that's referencing a DLL. I would like
Morning all, I know that this sounds like a simple referencing problem from the
First time posting here. I have problem about referencing a typedef struct from separate
i have problem to pass data from view to controller , i have view
I have problem like this https://dev.twitter.com/discussions/4563 if (webResponse.Headers[Content-Encoding] == gzip) { byte[] bytes =
i have a Question about referencing ParentEntities from Child Entites ir If i have
I have a nasty problem referencing resources when using a Maven project and Jar
I have an abstract base class that I'm referencing from one of my entities.
I am referencing OpenNETCF.Desktop.Communication (Rapi.dll) in my project and have the following problem: When
I have a problem With Entity Framework and TPC Inheritance With Self-Referencing Child Class.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.