Note that there are some explanatory texts on larger screens.

plurals
  1. POViewPager Activity to notify a Fragment of a specific event
    primarykey
    data
    text
    <p>I have a <code>ViewPager</code> and it is using a <code>FragmentAdapter</code> in order to display several fragments of the same kind. Although these Fragments are basically instantiated from the same class, they are using a <code>ListView</code> to display different information. (Obviously the <code>ListView</code> is being poulated by an <code>ArrayAdapter</code>.)<br> A background service is also running and is constantly receiving data from the Internet. I want to be able to update a specific Fragment in the <code>ViewPager</code> when my background service has notified me of a specific event.<br> How can I do that? A code snippet would be hugely appreciated!</p> <p>(By the way, I have saw <a href="https://stackoverflow.com/questions/6101958/how-should-a-fragment-get-notified-about-the-result-of-an-asynchronous-task">this similar question</a> but I have no idea how to use their suggestion!)</p> <p>To make it all more simple: My activity with the ViewPager:<br> <strong>[Fragment 0] [Fragment 1] [Fragment 2]</strong></p> <p>The background service tells me (via a broadcast) to update the <code>ListView</code> in <strong>Fragment 1</strong>. </p> <p><strong>EDIT:</strong> Here are sample codes: </p> <pre><code>public class ChatWindowPager extends FragmentActivity { private ViewPager mViewPager = null; private ChatFragmentAdapter mAdapter = null; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chat_window_pager); this.mViewPager = (ViewPager) findViewById(R.id.chatPager); this.mAdapter = new ChatFragmentAdapter(getSupportFragmentManager()); this.mViewPager.setAdapter(this.mAdapter); . . . } class ChatFragmentAdapter extends FragmentPagerAdapter implements ViewProvider { public ChatFragmentAdapter(final FragmentManager fm) { super(fm); } @Override public Fragment getItem(final int arg0) { String friendId = ..... // Some initializations ChatWindowFragment f = ChatWindowFragment.newInstance(friendId); return f; } @Override public int getCount() { ... } @Override public View getView(final int position) { View v = getLayoutInflater().inflate(R.layout.tab_holder, null); . . . return v; } } } </code></pre> <p>Now the fragments is defined like this: </p> <pre><code>public class ChatWindowFragment extends Fragment { public String friendId; private ListView lv; public static ChatWindowFragment newInstance(final String friendId) { ChatWindowFragment chatWindowFragment = new ChatWindowFragment(); Bundle bundle = new Bundle(); bundle.putString("friendId", friendId); chatWindowFragment.setArguments(bundle); return chatWindowFragment; } @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.friendId = getArguments().getString("friendId"); } @Override public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { View v = inflater.inflate(R.layout.chat_window, container, false); this.friendId = getArguments().getString("friendId"); . . . return v; } //The rest of the class } </code></pre> <p>As I am using a <code>FragmentPagerAdapter</code> I don't see how I can set the tag of each fragment! (Obviously, I am not using transactions to add the Fragments!)</p> <p><strong>EDIT 2:</strong> I would like to know whether what I'm doing, is the correct way to handle what I want to do... Any other solution is also welcome!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload