简体   繁体   中英

VIewPager display error when returns from another fragment

Here is the structure: I want to use only one Activity and several fragments to build my app. In the MainActivity there is a FrameLayout working as the container. At the very beginning Fragment_Base will be added to the container, in which there is a TabLayout and a ViewPager, containing three tabs: Tab1, Tab2 & Tab3, all fragments.

Then in Tab1 there is a button. When click it, the Framelayout(the container) will be replaced by a Fragment_Search. It works, indeed, and a new Fragment_Search displays. But when I click Back button on the phone and return to the former fragment, something's wrong.

Here is the change:

This is the original state of the Fragment_Base with 3 Tabs

Click the Button on Tab1 and jump to Fragment_Search

Return to Fragment_Base, and get wrong.

As you can see, all widgets in Tab1 disappear, but they show up again after I swipe to Tab2->Tab3->Tab2->Tab1. The background of Tab2 is orange, and it just disappears. One more weird thing is that the indicator bar of tab2 in TabLayout is in the wrong position, which also becomes normal if I swipe to tab3->tab2.

Here is some code:

  1. In MainActivity.java, OnCreate():

     if (findViewById(R.id.framelayout_base) != null) { if (savedInstanceState != null) { return ; } Frag_Base frag_base = new Frag_Base(); getSupportFragmentManager().beginTransaction() .add(R.id.framelayout_base, frag_base) .commit(); } 
  2. In Fragment_Base.java, OnCreateView():

     View view = inflater.inflate(R.layout.fragment_base, container, false); fragmentActivity = getActivity(); // initial views mViewPager = view.findViewById(R.id.viewPager_); mFragmentPagerAdapter = new MainFragmentPagerAdapter(fragmentActivity.getSupportFragmentManager()); mViewPager.setAdapter(mFragmentPagerAdapter); mTabLayout = view.findViewById(R.id.tabLayout_); mTabLayout.setupWithViewPager(mViewPager); mTabLayout.getTabAt(0).setIcon(R.drawable.ic_search); mTabLayout.getTabAt(1).setIcon(R.drawable.ic_star); mTabLayout.getTabAt(2).setIcon(R.drawable.ic_profile); 
  3. In MainFragmentPagerAdapter.java, getItem(int position):

     switch (position) { case 0: return new Fragment1(); case 1: return new Fragment2(); case 2: return new Fragment3(); default: return null; } 
  4. In Fragment1.java, OnClick(View view):

     case R.id.fab_gotoSearch: Log.d(TAG, "onClick: "+"go to search"); Frag_Search frag_search = new Frag_Search(); getFragmentManager().beginTransaction() .replace(R.id.framelayout_base, frag_search) .addToBackStack(null) .commit(); break; 

EDIT All logs are white, actually, but here is the logs of all fragments:

10-21 09:34:25.923 5537-5537/com.project.nanfang.music_n D/Fragment_Base: onAttach: 
10-21 09:34:25.923 5537-5537/com.project.nanfang.music_n D/Fragment_Base: onCreate: 
10-21 09:34:25.925 5537-5537/com.project.nanfang.music_n D/Fragment_Base: onCreateView: 
10-21 09:34:25.974 5537-5537/com.project.nanfang.music_n D/Fragment_Base: onStart: 
10-21 09:34:25.976 5537-5537/com.project.nanfang.music_n D/Fragment_Base: onResume: 
10-21 09:34:26.015 5537-5537/com.project.nanfang.music_n D/Fragment1: onAttach: 
10-21 09:34:26.015 5537-5537/com.project.nanfang.music_n D/Fragment1: onCreate: 
10-21 09:34:26.015 5537-5537/com.project.nanfang.music_n D/Fragment2: onAttach: 
10-21 09:34:26.015 5537-5537/com.project.nanfang.music_n D/Fragment2: onCreate: 
10-21 09:34:26.016 5537-5537/com.project.nanfang.music_n D/Fragment1: onCreateView: 
10-21 09:34:26.090 5537-5537/com.project.nanfang.music_n D/Fragment1: onStart: 
10-21 09:34:26.090 5537-5537/com.project.nanfang.music_n D/Fragment1: onResume: 
10-21 09:34:26.090 5537-5537/com.project.nanfang.music_n D/Fragment2: onCreateView: 
10-21 09:34:26.093 5537-5537/com.project.nanfang.music_n D/Fragment2: onStart: 
10-21 09:34:26.093 5537-5537/com.project.nanfang.music_n D/Fragment2: onResume: 
10-21 09:34:41.028 5537-5537/com.project.nanfang.music_n D/Fragment3: onAttach: 
10-21 09:34:41.028 5537-5537/com.project.nanfang.music_n D/Fragment3: onCreate: 
10-21 09:34:41.028 5537-5537/com.project.nanfang.music_n D/Fragment3: onCreateView: 
10-21 09:34:41.030 5537-5537/com.project.nanfang.music_n D/Fragment3: onStart: 
10-21 09:34:41.030 5537-5537/com.project.nanfang.music_n D/Fragment3: onResume: 
10-21 09:34:46.933 5537-5537/com.project.nanfang.music_n D/Fragment1: onPause: 
10-21 09:34:46.933 5537-5537/com.project.nanfang.music_n D/Fragment1: onStop: 
10-21 09:34:51.374 5537-5537/com.project.nanfang.music_n D/Fragment1: onCreateView: 
10-21 09:34:51.410 5537-5537/com.project.nanfang.music_n D/Fragment1: onStart: 
10-21 09:34:51.411 5537-5537/com.project.nanfang.music_n D/Fragment1: onResume: 
10-21 09:35:01.860 5537-5537/com.project.nanfang.music_n D/Fragment3: onPause: 
10-21 09:35:01.860 5537-5537/com.project.nanfang.music_n D/Fragment3: onStop: 
10-21 09:35:08.150 5537-5537/com.project.nanfang.music_n D/Fragment1: onClick: go to search
10-21 09:35:08.152 5537-5537/com.project.nanfang.music_n D/Frag_Search: onAttach: 
10-21 09:35:08.152 5537-5537/com.project.nanfang.music_n D/Frag_Search: onCreate: 
10-21 09:35:08.152 5537-5537/com.project.nanfang.music_n D/Fragment_Base: onPause: 
10-21 09:35:08.152 5537-5537/com.project.nanfang.music_n D/Fragment_Base: onStop: 
10-21 09:35:08.159 5537-5537/com.project.nanfang.music_n D/Frag_Search: onCreateView: 
10-21 09:35:08.243 5537-5537/com.project.nanfang.music_n D/Frag_Search: onStart: 
10-21 09:35:08.243 5537-5537/com.project.nanfang.music_n D/Frag_Search: onResume: 
10-21 09:35:13.843 5537-5537/com.project.nanfang.music_n D/Frag_Search: onPause: 
10-21 09:35:13.843 5537-5537/com.project.nanfang.music_n D/Frag_Search: onStop: 
10-21 09:35:13.854 5537-5537/com.project.nanfang.music_n D/Fragment_Base: onCreateView: 
10-21 09:35:13.874 5537-5537/com.project.nanfang.music_n D/Fragment_Base: onStart: 
10-21 09:35:13.874 5537-5537/com.project.nanfang.music_n D/Fragment_Base: onResume: 

Seems that after come back to Fragment_Base, Fragment1 doesn't call OnCreate(). IS this the problem?

好吧...我清理并重建项目,然后一切顺利...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM