我有一个包含FragmentStatePagerAdapter的活动,在这个FragmentStatePagerAdapter中有三个片段。或者,当调用正确的getItem()时,它们会被启动。
活动执行数据密集型任务,完成后发送广播。我在FragmentStatePagerAdapter中的每个片段中注册了一个广播接收器。
现在一切正常,除了一件事:
以下是我的一些代码示例:
FragmentStatePagerAdapter
公共类MessagePagerAdapter扩展了FragmentStatePagerAdapter {
public MessagePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
if(arg0 == 0) {
Fragment fragment = new RecentConversationFragment();
return fragment;
} else if(arg0 == 1) {
Fragment fragment = new PrivateConversationFragment();
return fragment;
} else {
Fragment fragment = new ClanConversationFragment();
return fragment;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
if(position == 0) {
return "Recent";
} else if(position == 1) {
return "Private";
} else {
return "Clan";
}
}
活动的相关部分
收到数据后,我广播了一个意图
Intent recentIntent = new Intent("MESSAGE_LOADER_RECENT");
MessageActivity.this.sendBroadcast(recentIntent);
这个广播收录在我的片段中:
private void createReceiver() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("MESSAGE_LOADER_RECENT");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
fetchMessages();
}
};
getActivity().getApplicationContext().registerReceiver(receiver,
intentFilter);
}
private void fetchMessages() {
this.conversations = mActivity.getRecentPrivateConversations();
messageListView.setAdapter(new ConversationListAdapter(mActivity,
R.layout.conversation_row, conversations));
messageListView.setDivider(null);
messageListView.setDividerHeight(0);
mActivity.recentConversationBroadcastReceived = true;
messageListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mActivity.navigateToConversation(conversations.get(position));
}
});
}
请注意,我设置了mActivity.recentConversationBroadcastReceived = true
,我这样做是因为我不断发送这样的广播:
在viewPager.setOnPageChangeListener中,
@Override
public void onPageSelected(int arg0) {
//here we check whether a broadcast is already sent to the
//specific fragment, if not we need to send one
if (arg0 == 0 || arg0 == 1) {
// recent or private
if (MessageActivity.this.loadedConversations != null) {
// check if the fragment already received a broadcast
if (arg0 == 0 && !recentConversationBroadcastReceived) {
Intent intent = new Intent("MESSAGE_LOADER_RECENT");
MessageActivity.this.sendBroadcast(intent);
} else if (arg0 == 1
&& !privateConversationBroadcastReceived) {
Intent intent = new Intent("MESSAGE_LOADER_PRIVATE");
MessageActivity.this.sendBroadcast(intent);
}
}
} else {
// clan
if (!clanConversationBroadcastReceived
&& MessageActivity.this.loadedClanConversations != null) {
Intent intent = new Intent("MESSAGE_LOADER_CLAN");
MessageActivity.this.sendBroadcast(intent);
}
}
}
我这样做是因为当我发送广播时,不保证当前活动片段是实际使用数据的片段,并且片段可能不会被启动。因此,我会在每次更改页面时发送广播,直到收到广告。
我希望有人可以帮我解决这个问题。
答案 0 :(得分:1)
唉。我讨厌回答自己的问题。
通过设置ViewPager的offScreenPageLimit可以避免此问题。默认限制是2,但我有3个片段,这就是为什么我的片段在0位每次都被重新创建的原因。所以解决方案是:
viewpager.setOffscreenPageLimit(2);
来源:Android : FragmentPagerAdapter don't saved the state of Fragment?
当然感谢帮助我的人。