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

  • Home
  • SEARCH
  • 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 8986373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:32:04+00:00 2026-06-15T21:32:04+00:00

I am having trouble implementing a feature in my Android app. Here’s the setup:

  • 0

I am having trouble implementing a feature in my Android app.

Here’s the setup:

  • ItemPagerActivity: An activity that contains a fragment that displays a pager.
  • ItemPagerFragment: The fragment containing a pager that loads other fragments. A cursor is used to load the fragments.
  • ItemFragment: The fragment in the pager, which performs an asynchronous task to load its data.

What I want is the following:

  • as a I swipe pages, the data in the currently displayed ItemFragment is communicated to the ItemPagerActivity (specifically, the name of the item will be used as the activity’s title).

I’ve defined a listener in ItemFragment that notifies when the data is loaded:

public class ItemFragment ... {

    public interface OnItemLoadedListener {
        public void onItemLoaded(Item item);
    }

    private Collection<OnItemLoadListener> listeners;

    private class LoadItemTask extends AsyncTask<...> {
        ...
        public void onPostExecute(Item item) {
            notifyItemLoaded(item);
            ...
        }
    }
}

If this fragment was wrapped by an Activity, then I could set the activity’s title simply by doing the following:

public class ItemActivity {

    public void onCreate(...) {
        ...
        ItemFragment fragment = new ItemFragment();
        fragment.registerItemLoadedListener(new ItemLoadedListener() {
            public void onItemLoaded(Item item) {
                setTitle("Item: " + item.getName());
            }
        });
        ...
    }
}

So that’s easy enough, and works as expected: when the activity starts, it creates the fragment, which loads the item, which notifies the activity, and the title is updated correctly.

But with ItemPagerFragment, the fragments are loaded pre-emptively: swiping to Fragment 3 may mean that Fragment 4 and Fragment 5 are created. Receiving notifications from the ItemFragment class when items are loaded is not correct here because the fragment displayed may not match the fragment that performed the last load.

Now the ViewPager class has a OnPageChangeListener which could be a solution: when I swipe, this listener is invoked with the current page number. From that page number, I need to (somehow) get the fragment representing that page from the adapter, get the Item data out of the fragment, and notify listeners that the Item is now loaded:

public class ItemPagerFragment ... {

    private Collection<OnItemLoadedListener> listeners;

    public View onCreateView(...) {
        ...
        ViewPager pager = (ViewPager) view.findViewById(R.id.pager):
        pager.setOnPageChangeListener(new OnPageChangeListener() {
            public void onPageChange(int pageNumber) {
                ItemFragment fragment = getItemFragment(pageNumber);
                Item item = fragment.getLoadedItem();
                notifyItemLoaded(item);
            }
         });
         ...
    }

}      

The ItemPagerActivity class would then register as a listener on the ItemPagerFragment class as follows:

class ItemPagerActivity ... {

    public void onCreate(...) {
        ...
        ItemPagerFragment fragment = new ItemPagerFragment();
        fragment.registerOnItemLoadedListener(new OnItemLoadedListener() {
            public void onItemLoaded(Item item) {
                setTitle("Item: " + item.getName());
            }
        });
        ...
    }
}

This looks good, but there are a number of problems:

  • The OnPageChangeListener may be invoked before a fragment has loaded its data (i.e., the fragment is swiped into view before the item has asynchronously loaded). So the call to fragment.getLoadedItem() may return null.

  • The OnPageChangeListener is not invoked for the initial page (only when a page changes, e.g. after a swipe action) so the activity title will be incorrect for the initial page.

  • The ViewPager class allows for only one OnPageChangeListener. This is a problem because I am also using the ViewPageIndicator library, which wants to assign a listener to the ViewPager.

I’m assuming that this pattern (notifying the activity of the data in a fragment that has been swiped into view) might be common, so I am wondering if there are any good solutions for this pattern, and to the three specific problems that I have identified above.

  • 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-15T21:32:05+00:00Added an answer on June 15, 2026 at 9:32 pm

    …so I am wondering if there are any good solutions for this pattern,
    and to the three specific problems that I have identified above.

    I don’t know if I would call it a pattern but the OnPageChangeListener is the way to go.

    The OnPageChangeListener may be invoked before a fragment has loaded
    its data (i.e., the fragment is swiped into view before the item has
    asynchronously loaded). So the call to fragment.getLoadedItem() may
    return null.

    First, your code should handle the “no data available situation” from the start. Your AsyncTasks will have the job of loading the data and also update the title only if the fragment for which they are working is the visible one(a position field in the ItemFragment tested against the ViewPager‘s getCurrentItem() method). The OnPageChangeListener will handle the update of the title after the data was loaded, as the user switches between pages and the data is available(it will return null if no data is available). To get the ItemFragment for a certain position you could use the code below:

        ItemFragment itf = getSupportFragmentManager()
                .findFragmentByTag(
                        "android:switcher:" + R.id.theIdOfTheViewPager + ":"
                                + position);
        if (itf != null) {
            Item item = fragment.getLoadedItem();
                notifyItemLoaded(item);
        }
    

    The OnPageChangeListener is not invoked for the initial page (only
    when a page changes, e.g. after a swipe action) so the activity title
    will be incorrect for the initial page.

    See above.

    The ViewPager class allows for only one OnPageChangeListener. This is
    a problem because I am also using the ViewPageIndicator library, which
    wants to assign a listener to the ViewPager

    I admit I don’t have much knowledge on the ViewPagerIndicator library but at a quick look on its site I saw:

    (Optional) If you use an OnPageChangeListener with your view pager you
    should set it in the indicator rather than on the pager directly.

    titleIndicator.setOnPageChangeListener(mPageChangeListener);

    I don’t see where is the limitation.

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

Sidebar

Related Questions

So I'm having trouble implementing a search bar in my app. The methods find
I'm having trouble implementing a QListWidget with custom items that can be reordered by
I'm having trouble implementing a dropbox backup to my app. I wan't the every
I have a small class hierarchy that I'm having trouble implementing copyWithZone: for. I've
I'm having trouble implementing this. From other questions I have the following snippet: jQuery.expr[:].Contains
I am having trouble implementing a sort. I have an XML document that I
I've been having trouble implementing the problem I've had here for a few days
I'm having trouble implementing this design I was given into HTML/CSS. Here is the
I'm having trouble implementing render to texture with OpenGL 3. My issue is that
I am implementing a filter feature for my application and having trouble with writing

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.