childFragments中的popBackStack()问题

时间:2018-12-08 19:46:03

标签: android android-fragments android-viewpager bottomnavigationview android-nested-fragment

我有问题。我有一个带有底部导航栏的应用程序-逻辑是在MainActivity中完成的,每个页面都是使用片段制作的。当我单击底部的那些按钮之一时,将打开另一个片段。在其中两个中,我嵌套了片段。在每个按钮中,我都有一个按钮,单击时会打开另一个片段(就像一种弹出窗口一样,该片段覆盖了屏幕的大部分区域,但应用栏除外)。打开弹出片段时,除了BACK BUTTON之外,一切都很好。当我单击它时,整个应用程序关闭。我试图从MainActivity中的堆栈中删除该弹出片段,但仍然无济于事。因此,通过应用程序的路径就是这样:

favourite_fragment-> favourite_sub_cars_fragment-> pop_up_fragment

您对如何使其正常工作有什么建议?在这里,我发布了一些必要的代码和一张按顺序排列的图像,以帮助您了解整个应用程序的逻辑。

app logic

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;

    private BottomNavigationView navigation;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {


        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {

                case R.id.navigation_main_page:
                    // close stack_search_fragment:
                    getSupportFragmentManager().popBackStack("stack_search_fragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    // close stack_favourite_sub_cars_fragment (-- IT ACTUALLY DOESN'T WORK --)
                    getSupportFragmentManager().popBackStack("stack_favourite_sub_cars_fragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    viewPager.setCurrentItem(0);
                    return true;

                case R.id.navigation_favourite:
                    // close stack_search_fragment:
                    getSupportFragmentManager().popBackStack("stack_search_fragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    // close stack_favourite_sub_cars_fragment (-- IT ACTUALLY DOESN'T WORK --)
                    getSupportFragmentManager().popBackStack("stack_favourite_sub_cars_fragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    viewPager.setCurrentItem(1);
                    return true;

                case R.id.navigation_search:
                    // close stack_search_fragment:
                    getSupportFragmentManager().popBackStack("stack_search_fragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    // close stack_favourite_sub_cars_fragment (-- IT ACTUALLY DOESN'T WORK --)
                    getSupportFragmentManager().popBackStack("stack_favourite_sub_cars_fragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    viewPager.setCurrentItem(2);
                    return true;

                case R.id.navigation_profile:
                    // close stack_search_fragment:
                    getSupportFragmentManager().popBackStack("stack_search_fragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    // close stack_favourite_sub_cars_fragment (-- IT ACTUALLY DOESN'T WORK --)
                    getSupportFragmentManager().popBackStack("stack_favourite_sub_cars_fragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    viewPager.setCurrentItem(3);
                    return true;

            }
            return false;
        }
    };
收藏夹中的

onClick()方法:

@Override
public void onClick(View v)
{
    try
    {
        Fragment newFragment = new PopUpFragment();
        FragmentTransaction tr = getFragmentManager().beginTransaction();

        tr.replace(R.id.fragmentContainerFavourite, newFragment);
        tr.addToBackStack("stack_favourite_sub_cars_fragment");
        tr.commit();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

fragment_favourite.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/fragmentContainerFavourite"
    >

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs_favourite"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?colorPrimary"
            app:tabIndicatorColor="?colorAccent"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="#ddd"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager_favourite"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/tabs_favourite"
        />

</android.support.constraint.ConstraintLayout>

fragment_favourite_sub_cars.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/containerFavouriteCars"
    >

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Go into pop-up fragment!"
        android:layout_marginTop="45dp"
        />

</android.support.v4.widget.NestedScrollView>

pop_up_frgment.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    >


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="ONE CAR!"
        android:textSize="20dp"/>

</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案