保留用于底部导航栏Android Studio(Java)的Backstack

时间:2020-03-31 13:50:56

标签: java android-studio android-fragments fragment-backstack android-nested-fragment

按照我的名字,我是新来的,需要帮助。

这是一个简单的应用程序,具有一个主要活动,其底部的导航栏用于导航到多个不同的片段。在主要活动中,单击导航栏中的R.id.nav_first / second分别将主要活动中的片段容器替换为第一个/第二个片段。

MAINACTIVITY.JAVA:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNav);
        findViewById(R.id.bottomNav);bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.nav_first:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragmentcontainer, new FirstFragment()).commit();
                    return true;
                case R.id.nav_second:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragmentcontainer, new SecondFragment()).commit();
                    return true;
        }
        return true;
    }});
}}

MAINACTIVITY.XML:

<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container">

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottomNav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:itemTextColor="@color/colorPrimaryDark"
    app:itemIconTint="@color/colorPrimaryDark"
    app:menu="@menu/nav"/>
<FrameLayout
    android:id="@+id/fragmentcontainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/bottomNav"/>

然后,我在FirstFragment中添加了一个按钮,用InnerFragment替换了FirstFragment,将FirstFragment添加到了Backstack。

FIRSTFRAGMENT.JAVA

public class FirstFragment extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.first_fragment, container, false);

    Button button = (Button) view.findViewById(R.id.next_button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            FragmentTransaction fr = getFragmentManager().beginTransaction();
            fr.replace(R.id.fragmentcontainer, new InnerFragment());
           fr.addToBackStack(TAG);
           fr.commit();
        }
});
    return view;}}

一切正常。但是,由于mainactivity.java的编码方式不同,当您处于InnerFragment中时,单击R.id.nav_second然后返回R.id.nav_first将带您进入FirstFragment,以直观的方式,作为用户,您需要它可以带您回到在“碎片堆栈”中离开的位置(InnerFragment)。

如何解决此问题,并使导航栏“记住”每个按钮的后盖? 从研究中我了解到,我应该将子片段嵌套在父片段A下。一些站点表示对片段使用数组列表,而其他站点则使用Viewpager。一般来说,我对解决问题和片段嵌套的最佳方法感到困惑。

在这里希望有人可以解决此问题的一些技巧,技巧甚至个人代码。如果可以分享,将不胜感激。谢谢!

0 个答案:

没有答案