Подтвердить что ты не робот

Фрагменты, перекрывающиеся друг с другом

У меня есть панель действий с 3 вкладками, каждая вкладка открывает фрагмент. На третьей вкладке "Каталог" есть список: enter image description here

Когда я нажимаю на элемент, он открывает другой фрагмент, который не является частью панели действий:

public void onClick(View v) {
    switch (v.getId())
    {
    case R.id.category1:    
        Fragment cosmeticsFragment = new ActivityCosmetics();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        transaction.replace(android.R.id.content, cosmeticsFragment);
        transaction.addToBackStack(null);

        transaction.setTransition(1);

        transaction.commit();
        break;
        ...

Вот как это выглядит после этого: enter image description here

С этого момента, если я перейду на другую вкладку и вернусь на вкладку "Каталог", я вижу, что два предыдущих фрагмента перекрывают друг друга:

enter image description here

Как я могу предотвратить это?

4b9b3361

Ответ 1

Вы можете управлять своими фрагментами, ища их по тегу. При добавлении фрагмента в backstack добавьте имя TAG

transaction.addToBackStack("myCustomFragmentTag");

Если вы хотите уничтожить фрагмент в любом месте приложения:

Fragment previousInstance = getFragmentManager().findFragmentByTag("myCustomFragmentTag");
                if (previousInstance != null)
                    transaction.remove(previousInstance);

Вы можете попробовать переопределить какое-либо поведение, чтобы эта строка кода уничтожала инициализированный последний фрагмент

getFragmentManager().popBackStack(); 

Ответ 2

Настройка фона для фрагментации будет исправлена.

Ответ 4

Вы можете попробовать это для вкладок.

   public class Tabs extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tabs);
    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
   // Intent intent;  // Reusable Intent for each tab
    // Create an Intent to launch an Activity for the tab (to be reused)
    Intent intent1 = new Intent().setClass(this, Social.class);
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("app_name").setIndicator("Practice",
                      res.getDrawable(R.drawable.tab_social))
                  .setContent(intent1);
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.bbgg);
    // Do the same for the other tabs
    Intent intent2 = new Intent().setClass(this, Web.class);
    spec = tabHost.newTabSpec("application").setIndicator("Application",
                      res.getDrawable(R.drawable.tab_web))
                  .setContent(intent2);
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.bbgg);
    Intent intent3 = new Intent().setClass(this, Catalog.class);
    spec = tabHost.newTabSpec("toplinks").setIndicator("Top Links",
                      res.getDrawable(R.drawable.tab_catalog))
                  .setContent(intent3);
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.bbgg);
    tabHost.setCurrentTab(0);
}
}

В вашем макете xml

    <TabHost 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

  <LinearLayout 
android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:orientation="vertical">
  <TabWidget 
android:id="@android:id/tabs" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"></TabWidget>
 <FrameLayout 
android:id="@android:id/tabcontent" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"></FrameLayout>
 </LinearLayout>
 </TabHost>