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 8419073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:30:36+00:00 2026-06-10T02:30:36+00:00

My problem is to initially load the fragment directly and also I want to

  • 0

My problem is to initially load the fragment directly and also I want to call a method fragment is visible to the user.

For loading the correct fragment, I use viewPager.setCurrentItem() in onCreate. It works well.

FragmentActivity code:

public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    mTabsAdapter = new TabsPlayerAdapter(this, mRadioList);
    mMainViewPager.setAdapter(mTabsAdapter);
    mMainViewPager.setOnPageChangeListener(this);
    mMainViewPager.setCurrentItem(2);
}

To a method called fragment Current, I use the onPageSelected viewPager. It works well.

FragmentActivity code:

@Override
public void onPageSelected(int position) {
    Log.d(TAG, "onPageSelected - position: " + position);
    ((PlayerFragment) mTabsAdapter.mSparseFragment.get(position))
                                  .startAnnimation();
}

FragmentStatePagerAdapter code:

@Override
public Fragment getItem(final int position) {
    Log.d(TAG, "getItem - position: " + position);
    Bundle args = new Bundle();  
        args.putString("num", mRadioModel.get(position).name);
        mSparseFragment.put(position, Fragment.instantiate  
                    (mContext,PlayerFragment.class.getName(), args));
        return mSparseFragment.get(position);
}

FragmentStatePagerAdapter code :

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

I can do these two things separately. But when I try to use the two together, I have a "NullPointerException" when I call my fragment.
I saw that onPageSelected is called before the getItem in my Adapter.

  • 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-10T02:30:37+00:00Added an answer on June 10, 2026 at 2:30 am

    You’ll know that items are available when onAttachedToWindow is called. At the moment you call setCurrentItem, the adapter has not been used to create views yet. (setAdapter is not sufficient to know that all have been inflated)

    Else, you can consider setting your listener after setting the currentItem, therefore not having a animation when opening the fragment.

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

Sidebar

Related Questions

I am having problem using mvc:resources in spring 3.1 configuration. Initially i was working
I was trying to solve this problem - http://www.spoj.pl/problems/LISA/ I thought of Greedy initially,
Problem: Been struggling to get my code to load external shaders and it is
I've reviewed some posts about lazy loading but I believe my problem is a
I initially thought this was a problem with my .htaccess files, however even after
I had this problem initially, so I tried npm install --force It seems to
The problem is that I am not sure how to load the data from
I am having a problem when I load images from my SD card and
I face a problem in the following code which is used to load an
I have a page with a WYSIWYG editor the user can use. After editing,

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.