可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am facing a problem of overlapping fragments when i switch between tabs and attach fragments to a tab view below is my code please help
public class FragmentManage extends Fragment implements ActionBar.TabListener { private Fragment mFragment; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_manage, container, false); OnClickListener clickListener = new OnClickListener() { public void onClick(View v) { FragmentTransaction ft = getFragmentManager().beginTransaction(); switch(v.getId()) { case R.id.imageBtnCategory: if (mFragment == null){ mFragment = new FragmentCategory(); } ft.replace(android.R.id.content, mFragment); break; case R.id.imageBtnManageKey: if (mFragment == null){ mFragment = new FragmentKeys(); } ft.replace(android.R.id.content, mFragment); break; case R.id.imageBtnChangePswd: if (mFragment == null){ mFragment = new FragmentChangePwd(); } ft.replace(android.R.id.content, mFragment); break; } ft.commit(); } }; ImageButton imageBtnCategory = (ImageButton) v.findViewById(R.id.imageBtnCategory); ImageButton imageBtnManageKey = (ImageButton) v.findViewById(R.id.imageBtnManageKey); ImageButton imageBtnChangePswd = (ImageButton) v.findViewById(R.id.imageBtnChangePswd); imageBtnCategory.setOnClickListener(clickListener); imageBtnManageKey.setOnClickListener(clickListener); imageBtnChangePswd.setOnClickListener(clickListener); return v; } public void onTabSelected(Tab tab, FragmentTransaction ft) { mFragment = new FragmentManage(); ft.add(android.R.id.content, mFragment); ft.attach(mFragment); } public void onTabUnselected(Tab tab, FragmentTransaction ft) { ft.remove(mFragment); } public void onTabReselected(Tab tab, FragmentTransaction ft) { } }
回答1:
Just set a background color to your <fragment />
in XML file.
Solve this problem.
回答2:
I may be very late to answer this question.
Note:
This answer may not be related to the above question, But hope it will help for some.
Sometimes, Fragment overlap issue occurs when we try to use the different type of fragments( i.e using support fragment's in few fragments and normal fragments in someother fragment )
Recently i faced the same problem in Navigation drawer. By mistake, i used "import android.support.v4.app.Fragment;" in one fragment, And used "import android.app.Fragment;" in few other fragment.
Hope this will help for somebody..
回答3:
fragment manager maintains the stack of all the previous fragments that are replaced sometimes the back stack fragments overlaps with the fragment we replaced, for me
fragmentManager.popBackStack();
works, we can do this in a loop too to pop all the fragments in the stack hope it helps, Thanks. Happy Coding :)
回答4:
Well Setting up fragment background color is not a solution because fragment will be still in the activity stack which may consume memory.
Solution would be remove all views from your framelayout before adding any new fragment.
private void changeFragment(Fragment fr){ FrameLayout fl = (FrameLayout) findViewById(R.id.mainframe); fl.removeAllViews(); FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction(); transaction1.add(R.id.mainframe, fr); transaction1.commit(); }
回答5:
This is how I fixed it ..
Setting the background would remove the overlapping effect from the screen only if the layout is matched to fill screen
New fragments replaced on button clicks were not getting replaced on tab_selected or tab change action
following code fixed it for me
public void onTabUnselected(Tab tab, FragmentTransaction ft) { // This is required since button click replaces fragment whose link is lost hence overlapping isuee was occuring ft.replace(android.R.id.content, mFragment); // On Tab Change remove old fragment views ft.remove(mFragment); }
回答6:
Another problem can be related to using android.R.id.content
as a container. I've just created FrameLayout
and use id from there.
回答7:
I also faced fragment overlapping issue.Here is how I solved it -
1) We need to add the first fragment with addToBackStack, so that it is retained in the stack -
FirstFragment firstFragment = new FirstFragment(); getFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).addToBackStack("first frag").commit();
2) While adding the second fragment, replace the first fragment rather then adding it.Since the first fragment was already added in the stack, so it will be present when you press back from second fragment -
SecondFragment secondFragment= new SecondFragment(); getFragmentManager().beginTransaction().replace(R.id.fragment_container, secondFragment).addToBackStack("second frag").commit();
3) Here is how back press can be handled, below code should be present in the parent Activity -
public void onBackPressed(){ if(getFragmentManager().getBackStackEntryCount() <= 1){ super.onBackPressed(); } else { getFragmentManager().popBackStack(); } }
回答8:
private void changeFragment(Fragment fr){ FrameLayout fl = (FrameLayout) findViewById(R.id.mainframe); fl.removeAllViews(); FragmentTransaction transaction1 =getSupportFragmentManager().beginTransaction(); transaction1.add(R.id.mainframe, fr); transaction1.commit();}
回答9:
when you have a overlap fragment, maybe your background of your fragment is transparent, you need put a android:background="@color/white"' inside your fragment propeties
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white"
and white you need put inside of the colors.xml #FFFFFF in the rest folder.
回答10:
I have sometimes the same problem, but my problem is not related to different fragment managers (getSupportFragmentManager()
, getFragmentManager()
). I think there is still another problem. In my case, when I open the navigation drawer, I always delete old fragments on every menu option, for example:
Fragment calendarFragment = context.getSupportFragmentManager().findFragmentByTag(FragmentTag.CALENDAR.name()); if (calendarFragment != null) { context.getSupportFragmentManager().beginTransaction().remove(calendarFragment).commit(); }
It's not possible to get into a sub menu without the navigation drawer, so the fragments are basically always removed. In some cases, after I remove and add a fragment again inside an Activity it suddenly overlaps with another (previous) fragment?! But why does the fragment manager suddenly finds previous fragments? That can either mean that the fragment manager is buggy and it does not remove old fragments, or that something other is broken.
My thought is, that it's somehow the fault of Android Studio or any other adb development tool which is used by it. Why I think that is, because Android Studio sometimes seem to loose the instance of the running app. Probably this problem is somehow related to the same issue: Android Studio not deploying changes to app. I didn't figure out yet when this happens. What I know is that it can't be a programming issue, because it's not reproducible after I relaunched the app by Android Studio. I guess there are somehow any background processes hanging which causes multiple instances of Activities, Fragments, fragment managers and so on. Furthermore it's not just this bug. I saw a lot of strange behaviours in past which are similar to this one. (For example the behaviours suddenly disappeared when the app was not started by IDE).
回答11:
It all has to do with the engineering behind the replace and the addToBackStack methods.
The replace method will actually do two things. Hide the existing fragment (let's say A) and add a new one (let's say B) in the container.
Adding that transaction to the back stack will do the opposite. Remove B and add A.
So, the solution to your problem is
1. use addToBackStack method.
2. make sure that the reverse replacement of fragments is the correct order
The second suggestion is really important, because it is pretty often that at some point the current fragment is not removed and another one is added, which causes the two fragments showing at the same time.
回答12:
Making the background color white solved it for me too. I think this is a bug or and implementation issue in the Android fragmentment manager. I was implementing my replace and popBackStack() correctly.
回答13:
When I had a problem like this it appeared that I was adding one fragment with childFragmentManager and another with parent's fragmentManager, so check that you use same type of fragment manager.