如何避免反复替换相同的片段?

时间:2017-03-19 14:22:15

标签: android android-fragments navigation-drawer

我正在创建我的第一个Android应用程序。完成所需的作业后,我带来这里寻求帮助。

可以从导航抽屉调用片段A.再次,如果我滑动导航栏并调用片段A.它的重叠,以前的Invoked Fragment A.问题是用户必须再次点击两次才能进入主屏幕。

我分享了一些屏幕截图和摘要。

启动片段A(Help_Fragment())

 private void createHelp() {

    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    Log.d(taglaunch, "Switching to Help Fragment");
    Fragment fragment = new Help_Fragment();
    fragmentTransaction.setCustomAnimations(R.animator.slide_in_left,
            R.animator.slide_out_right, R.animator.exit_to_left, R.animator.exit_to_right);
    fragmentTransaction.replace(R.id.app_bar, fragment).addToBackStack(null).commit();

}

的onCreate

 protected void onCreate(Bundle savedInstanceState) {
    Log.d(taglaunch, "on Create - AppDrawer");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_appdrawer);
    mainActivityAdapter = new MainActivityAdapter(getFragmentManager());
    navigate();
    switchtofragment();
    getFragmentManager().addOnBackStackChangedListener(backStackListener);

}

 public void switchtofragment() {
        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("TAB1"));
        tabLayout.addTab(tabLayout.newTab().setText("TAB2"));
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(mainActivityAdapter);
        tabLayout.setOnTabSelectedListener(this);
        tabLayout.setupWithViewPager(viewPager);

    }

public void navigate() {

    toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

}

Fragment A screenshot.

Navigation Drawer

2 个答案:

答案 0 :(得分:1)

  

如果片段存在,您可以使用以下代码替换片段。

public void loadFragment(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, fragment);
        transaction.commitAllowingStateLoss();
    }

答案 1 :(得分:0)

您可以使用Fragment Manager的findFragmentByTag()方法检查Help_Fragment是否已存在。如果它不存在,那么您可以继续创建一个。除此之外,替换方法替换以编程方式添加的片段,即替换为像框架布局的容器。但这个解决方案可能只是一个解决方案。试试看,告诉我们。 :)

代码: -

FragmentManager manager=getSupportFragmentManager();
    Help_Fragment fragment1= (Help_Fragment) manager.findFragmentByTag("Help_Fragment");
    if(fragment1==null)
    {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        Log.d(taglaunch, "Switching to Help Fragment");
        Fragment fragment = new Help_Fragment();
        fragmentTransaction.setCustomAnimations(R.animator.slide_in_left,
                R.animator.slide_out_right, R.animator.exit_to_left, R.animator.exit_to_right);
        fragmentTransaction.replace(R.id.app_bar, fragment,"Help_Fragment").addToBackStack(null).commit();
    }
    else{
        // Display a Toast that fragment exists
    }