片段有时会重叠-Android

我有一个导航抽屉,然后单击显示/隐藏/创建全屏片段的项目。

在大多数情况下,此代码效果很好。但是有时候,也许有1%的时间,当我已经运行该应用程序时,我会疯狂地全屏重叠片段。

我的代码有问题吗?还是Android中的其他功能无法识别我已经创建了带有标签的片段?

这是有关我如何显示/隐藏/创建片段的相关代码:

@SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {

        // Get to drawer layout so we can interact with it
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        // Get the fragment manager to remove/add fragments
        FragmentManager fragmentManager = getSupportFragmentManager();

        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_profile) {

            // Hide visible fragment
            fragmentManager.beginTransaction().hide(getVisibleFragment()).commit();


            // Check if the fragment exists first.
            if(fragmentManager.findFragmentByTag("profileFragment") != null) {

                // If the fragment exists, show it (no reason to recreate it).
                fragmentManager.beginTransaction()
                        .show(fragmentManager.findFragmentByTag("profileFragment"))
                        .commit();
            } else {
                // If the fragment does not exist, add it to fragment manager with a tag to identify it.

                // Create new fragment instance with required argument(s).
                ProfileFragment fragment = ProfileFragment.newInstance();

                fragmentManager.beginTransaction()
                        .add(R.id.content_frame, fragment, "profileFragment")
                        .commit();
            }

            // Set the title
            mToolbarTitleTextView.setText(R.string.title_activity_profile);

        } else if (id == R.id.nav_feed) {

            // Hide visible fragment
            fragmentManager.beginTransaction().hide(getVisibleFragment()).commit();


            // Check if the fragment exists first.
            if(fragmentManager.findFragmentByTag("feedFragment") != null) {
                // If the fragment exists, show it (no reason to recreate it).
                fragmentManager.beginTransaction()
                        .show(fragmentManager.findFragmentByTag("feedFragment"))
                        .commit();
            } else {
                // If the fragment does not exist, add it to fragment manager with a tag to identify it.
                fragmentManager.beginTransaction()
                        .add(R.id.content_frame, new feedFragment(), "feedFragment")
                        .commit();
            }

            // Set the title
            mToolbarTitleTextView.setText(R.string.title_activity_feed);

        } else if (id == R.id.nav_notifications) {

            // Hide visible fragment
            fragmentManager.beginTransaction().hide(getVisibleFragment()).commit();

            // Hide the post button
            mPostButton.setVisibility(View.GONE);

            // Check if the fragment exists first.
            if(fragmentManager.findFragmentByTag("notificationsFragment") != null) {
                // If the fragment exists, show it (no reason to recreate it).
                fragmentManager.beginTransaction()
                        .show(fragmentManager.findFragmentByTag("notificationsFragment"))
                        .commit();
            } else {
                // If the fragment does not exist, add it to fragment manager with a tag to identify it.
                fragmentManager.beginTransaction()
                        .add(R.id.content_frame, new NotificationsFragment(), "notificationsFragment")
                        .commit();
            }

            // Set the title
            mToolbarTitleTextView.setText(R.string.title_activity_notifications);

        }

        mDrawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    // Useful method to hide the currently visible fragment
    public Fragment getVisibleFragment(){
        FragmentManager fragmentManager = MainActivity.this.getSupportFragmentManager();
        List<Fragment> fragments = fragmentManager.getFragments();
        if(fragments != null){
            for(Fragment fragment : fragments){
                if(fragment != null && fragment.isVisible())
                    return fragment;
            }
        }
        return null;
    }

编辑:真的很难重现此错误,这使其很难调试。这似乎是随机发生的。

这是我解决问题的方法。在我的MainActivity中,我这样做:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    setContentView(R.layout.activity_main);
}

基本上发生了什么,如果我在屏幕上有1个以上的片段,如果android系统在应用程序处于后台时资源不足,并在恢复MainActivity.onCreate()后将其关闭,则会被调用,并将重新实例化所有片段与电话

super.onCreate(savedInstanceState);

因此,我只是将其设置为null,这阻止了所有这些片段的重新创建。

它们重叠的原因是因为它们都被立即显示。

绝对不是正确的方法,但现在可以解决我的问题= P

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android低功耗蓝牙有时会锁定

来自分类Dev

滚动时Android Studio有时会滞后

来自分类Dev

BootStrap映像有时会重叠

来自分类Dev

当应用恢复时,Android EditText有时会丢失输入

来自分类Dev

库清单中的android:supportsRtl =“ true”是必不可少的吗?有时会导致错误

来自分类Dev

在Android上连接到特定的wifi有时会失败

来自分类Dev

Android Studio有时会使用已弃用的“ fill_parent”

来自分类Dev

Android:设置颜色滤镜时,按钮背景XML有时会丢失alpha

来自分类Dev

我的Xamarin.Android应用有时会在启动时崩溃

来自分类Dev

Android:删除硬件文件有时会使手机变砖

来自分类Dev

使用Android Firebase Analytics SDK时有时会丢失事件参数

来自分类Dev

Android:删除硬件文件有时会使手机变砖

来自分类Dev

Android应用有时会在启动时崩溃

来自分类Dev

Android Studio有时会使用已弃用的“ fill_parent”

来自分类Dev

Android应用程序有时会启动错误的活动

来自分类Dev

使用SubsamplingScaleImageView的Android ViewPager在滑动图像时有时会出现空白屏幕

来自分类Dev

我的Xamarin.Android应用有时会在启动时崩溃

来自分类Dev

由于Robotium无法找到ImageButton对象,Android UI测试有时会失败

来自分类Dev

为什么Android Beacon Library didRangeBeaconsInRegion 有时会触发两次?

来自分类Dev

Quart.Net 有时会运行重叠的任务

来自分类Dev

使用流式传输的Android File To Base64有时会丢失2个字节

来自分类Dev

Android-奇怪的应用程序小部件行为:有时会自身擦除其内容

来自分类Dev

为什么nmap似乎无法查看我的android设备,有时会看到它们,但有时却看不到

来自分类Dev

有时会恢复,有时会结帐

来自分类Dev

android中的Geocoder有时会在某些设备上显示“服务不可用错误”。怎么处理呢?

来自分类Dev

Arduino - Android 蓝牙连接有时会出现第一个字符错误

来自分类Dev

执行片段交易时,主屏幕有时会闪烁

来自分类Dev

mmap有时会失败

来自分类Dev

CIGaussianBlur有时会更改imageOrientation

Related 相关文章

  1. 1

    Android低功耗蓝牙有时会锁定

  2. 2

    滚动时Android Studio有时会滞后

  3. 3

    BootStrap映像有时会重叠

  4. 4

    当应用恢复时,Android EditText有时会丢失输入

  5. 5

    库清单中的android:supportsRtl =“ true”是必不可少的吗?有时会导致错误

  6. 6

    在Android上连接到特定的wifi有时会失败

  7. 7

    Android Studio有时会使用已弃用的“ fill_parent”

  8. 8

    Android:设置颜色滤镜时,按钮背景XML有时会丢失alpha

  9. 9

    我的Xamarin.Android应用有时会在启动时崩溃

  10. 10

    Android:删除硬件文件有时会使手机变砖

  11. 11

    使用Android Firebase Analytics SDK时有时会丢失事件参数

  12. 12

    Android:删除硬件文件有时会使手机变砖

  13. 13

    Android应用有时会在启动时崩溃

  14. 14

    Android Studio有时会使用已弃用的“ fill_parent”

  15. 15

    Android应用程序有时会启动错误的活动

  16. 16

    使用SubsamplingScaleImageView的Android ViewPager在滑动图像时有时会出现空白屏幕

  17. 17

    我的Xamarin.Android应用有时会在启动时崩溃

  18. 18

    由于Robotium无法找到ImageButton对象,Android UI测试有时会失败

  19. 19

    为什么Android Beacon Library didRangeBeaconsInRegion 有时会触发两次?

  20. 20

    Quart.Net 有时会运行重叠的任务

  21. 21

    使用流式传输的Android File To Base64有时会丢失2个字节

  22. 22

    Android-奇怪的应用程序小部件行为:有时会自身擦除其内容

  23. 23

    为什么nmap似乎无法查看我的android设备,有时会看到它们,但有时却看不到

  24. 24

    有时会恢复,有时会结帐

  25. 25

    android中的Geocoder有时会在某些设备上显示“服务不可用错误”。怎么处理呢?

  26. 26

    Arduino - Android 蓝牙连接有时会出现第一个字符错误

  27. 27

    执行片段交易时,主屏幕有时会闪烁

  28. 28

    mmap有时会失败

  29. 29

    CIGaussianBlur有时会更改imageOrientation

热门标签

归档