恢复活动时重叠片段 [英] Overlapping Fragments when resuming Activity

查看:92
本文介绍了恢复活动时重叠片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用程序有问题,我正在使用Android STUDIO IDE进行开发. 当我将应用程序在后台停留几分钟,或者被系统杀死,或者我混合了片段的不同布局时,几乎是什么时候. 我在下面放了一张图片:

I have a problem with my android app, I'm developing with Android STUDIO IDE. Pretty much when I leave the app in the background for a few minutes, or is killed by the system or I mix the different layouts of the fragment. I have put a picture below:

如果您还有其他编写方法,我已经尝试了多种方法.预先谢谢你.

I've already tried a variety of methods, if you have others write as well. Thank you in advance.

super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tl = new ActionBar.TabListener() {
            @Override
            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

            }

            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                pager.setCurrentItem(tab.getPosition());
                actionBar.setSelectedNavigationItem(tab.getPosition());
            }

            @Override
            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

            }
        };

        String label1 = getResources().getString(R.string.label1);
        ActionBar.Tab tab;
        tab = actionBar.newTab();
        tab.setText(label1);

        tab.setIcon(R.drawable.download);


        //tab.setIcon(R.drawable.data);
        tab.setTabListener(tl);
        actionBar.addTab(tab);

        String label2 = getResources().getString(R.string.label2);
        tab = actionBar.newTab();
        tab.setText(label2);
        tab.setIcon(R.drawable.search);


        tab.setTabListener(tl);
        actionBar.addTab(tab);

        String label3 = getResources().getString(R.string.label3);
        tab = actionBar.newTab();
        tab.setText(label3);
        tab.setIcon(R.drawable.television);

        tab.setTabListener(tl);
        actionBar.addTab(tab);

图片:

推荐答案

所以问题是您在onCreate中添加了片段A.导航到片段B后,您的活动进入了后台.如您所知,在某些情况下,android会杀死您的后台活动,并在涉及前台时强制其重新创建自己.因此,您的活动将使用片段B重新创建自己,片段B是进入后台时的最后状态,并添加片段A,因为调用了onCreate.您可以通过在onCreate中执行一次saveedinstancestate检查来解决此问题.

So the problem is that you have fragment A added in your onCreate. After navigating to fragment B your activity goes into the background. In certain cases as you know android can kill your background activity and force it to recreate itself when it comes to the foreground. Thus your activity recreates itself with fragment B which was its last state when it went into the background, as well as adds fragment A because onCreate was called. You can solve this by doing a savedinstancestate check in your onCreate.

这篇关于恢复活动时重叠片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆