替换片段仍可见 [英] Replaced fragment still visible

查看:117
本文介绍了替换片段仍可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始了我的应用程序,它运行的的AsyncTask 来加载,然后在 onPostExecute ,然后我的setContentView 到新的布局,然后添加一个片段,有两个按钮,提供两种模式通过添加 FragmentTransaction 。点击这两个模式之一后,并将其替换为另一个 FragmentTransaction 使用替换法的片段。

When I start my app it runs an AsyncTask to load up and then in onPostExecute, I then setContentView to the new layout then add a fragment with two buttons offering two modes by an add FragmentTransaction. After one of the two modes is clicked, it then replaces the fragment with yet another FragmentTransaction using the replace method.

如果该应用程序崩溃,它返回到第一屏,装载了两个按钮提供了两种模式。在不管选择哪一模式,该情况下,第二片段被加载,但现在是在背景被突然透明示出下面的两个按钮和它们保持点击。如果它们被再次点击他们适当地更换该片段使得它是不可见下文。这仅仅是奇怪的,我不明白这是什么原因。

If the app crashes it returns to the first screen, loading up the two buttons offering the two modes. In this case if either mode is selected, the second fragment is loaded but is now the background is suddenly transparent showing the two buttons below and they remain clickable. If they are clicked again they properly replace the fragment so that it isn't visible below. This is just weird, I can't understand what could cause this.

我研究过,看到这两个类似的问题,​​ 之一和<一href="http://stackoverflow.com/questions/12958555/android-replace-fragment-still-displays-some-of-the-replaced-fragment">two,这表明,这可能是因为ID是错误的或者我已经定义了XML中的片段。既不这两个因素是这种情况。

I've researched and seen these two similar questions, one and two, which suggested that it might be because the ID is wrong or I have defined the fragment in XML. Neither of these two factors are the case.

我的code如下所示:

My code is shown below:

下面我更换加载屏幕。

Below I replace the loading screen.

@Override
    protected void onPostExecute(Void result) {
        setContentView(R.layout.activity_main_screen);
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.add(R.id.fragment_container, new ModeFragment())
                .commit();
            }

后,当点击一个按钮,我通过片段我想替换到下面这个方法目前的:

After which, when a button is clicked I pass the fragment I wish to replace the current with into this method below:

private void replaceCurrentFragment(Fragment fragment) {
    FragmentTransaction transaction = getSupportFragmentManager()
            .beginTransaction();
    transaction.replace(R.id.fragment_container, fragment)
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .addToBackStack(null).commit();
}

这个作品首次,但是,如果发生崩溃然后该应用返回到第一片段和该方法中被传递的第二时间,新的替换片段是半不可见的。点击第一个片段上的按钮即可再次调用这个方法,现在是很好。

This works the first time, however if a crash occurs then the app returns to the first fragment and the second time this method is passed, the new replacing fragment is semi-invisible. Clicking the button on the first fragment again calls this method again and it is now fine.

很显然,我不希望应用程序崩溃所以这不应该发生,但我得到这种感觉,有什么毛病我怎么写我的code。

Obviously I don't want the app to crash so this shouldn't occur, but I get this feeling that there's something wrong with how I'm writing my code.

推荐答案

我有同样的问题发生在我身上,这是因为我装在我的活动的OnCreate片段,而不检查,如果有一个savedInstanceState ,因此Android第一重开所有的旧片段,然后执行OnCreate中,其中添加的片段在旧的无需更换它们所以,当您导航到另一个片段,它只是取代了上面的一个,但不是下一个,所以你会看到其下的片段。

I've had the same problem happen to me, and it was because I loaded a fragment in the OnCreate of my Activity, without checking if there was a savedInstanceState, so android first reopen all old fragments, then do the OnCreate, which added the fragment over the old ones without replacing them so when you navigate to another fragment, it only replaces the top one, but not the bottom one, so you will see the fragments under it.

可能不完全一样的东西给你,但它可以帮助你看着办吧。

Might not be exactly the same thing for you, but it might help you figure it out.

这篇关于替换片段仍可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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