每次oncreateView片段时从活动中检索数据
Posted
技术标签:
【中文标题】每次oncreateView片段时从活动中检索数据【英文标题】:Retrive data from activity every time oncreateViwe of fragments 【发布时间】:2021-03-30 08:04:54 【问题描述】:活动
public class GroupesActivity extends BaseActivity
SelectedBundle selectedBundle;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_groupes);
sectionsPagerAdapter = new GroupPagerAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(sectionsPagerAdapter);
tabs.setupWithViewPager(viewPager);
private void getAllGroup()
// api call retrive data
// send data using interface on response
// set data selectedBundle.onBundleSelect(isVisible,calanderModelList,groupModelList,eventModelList);
public void setOnBundleSelected(SelectedBundle selectedBundle)
this.selectedBundle = selectedBundle;
public interface SelectedBundle
void onBundleSelect(boolean isVisible, List<CalanderModel> calanderModelList, List<GroupModel> groupModelList, List<EventModel> eventModelList);
片段
public class FragmentOne extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_all_groups_fragments, container, false);
// get data only once on oncreateview
((GroupesActivity) getActivity()).setOnBundleSelected(new GroupesActivity.SelectedBundle()
@Override
public void onBundleSelect(boolean isVisible, List<CalanderModel> calanderModelListtt, List<GroupModel> groupModelList, List<EventModel> eventModelList)
Log.e("retrive data","data")
);
return root;
GroupPagerAdapter
public class GroupPagerAdapter extends FragmentPagerAdapter
@StringRes
private static final int[] TAB_TITLES = new int[]R.string.tab_text_1, R.string.tab_text_2, R.string.tab_text_3;
private final Context mContext;
public GroupPagerAdapter(Context context, FragmentManager fm)
super( fm);
this.mContext=context;
@NonNull
@Override
public Fragment getItem(int position)
switch (position)
case 0:
return new AllGroupsFragments();
case 1:
return new HostFragments();
case 2:
return new GuestFragments();
return null;
@Nullable
@Override
public CharSequence getPageTitle(int position)
return mContext.getResources().getString(TAB_TITLES[position]);
@Override
public int getCount()
return 3;
问题
当我滑动这个上面的片段时,我在 tabview 中有三个片段 然后在这个没有调用接口的fragmnet中再次出现,我是 没有从活动中获取数据
如何从其父活动中再次获取该数据,我只需要 从片段 oncreateView 的每次活动中检索该数据
提前感谢 ;)
【问题讨论】:
【参考方案1】:在 Fragment 中使用setMenuVisibility()
方法来解决问题。
//In fragments
@Override
public void setMenuVisibility(final boolean isVisible)
super.setMenuVisibility(isVisible);
if (isVisible)
//visible to user- do ur stuff
((GroupesActivity) getActivity()).setOnBundleSelected(new
GroupesActivity.SelectedBundle()
@Override
public void onBundleSelect(boolean isVisible, List<CalanderModel>
calanderModelListtt, List<GroupModel> groupModelList, List<EventModel>
eventModelList)
Log.e("retrive data","data")
);
如果您遇到任何问题,以下链接可能会有所帮助: How to determine when Fragment becomes visible in ViewPager
【讨论】:
感谢您的快速回答,但它给出了 NullPointerException: on interface method with on a null object reference at setOnBundleSelected 可以分享一下logcat日志吗? 问题是当我通过滑动 tabview 继续片段 3 并再次进入片段 1 时它再次创建并且那个时候这个接口方法没有调用 java.lang.NullPointerException:尝试在 com.illumaworks.Whenis 的空对象引用上调用虚拟方法“void com.activity.GroupesActivity.setOnBundleSelected(com.activity.GroupesActivity$SelectedBundle)”。 Fragments.AllGroupsFragments.setMenuVisibility(AllGroupsFragments.java:96) fragment创建时有没有其他方法可以再次从父activity获取数据?以上是关于每次oncreateView片段时从活动中检索数据的主要内容,如果未能解决你的问题,请参考以下文章
替换未调用 onAttach、onCreate、onCreateView 等的活动中的片段
Android 片段 onCreateView 与 onActivityCreated
完成 onCreate 中的活动时,如何在 onCreate 之前调用我的片段的 onCreateView?