FragmentTransaction.Replace造成白色(空白?)屏幕中的应用程序

时间:2019-01-31 19:59:15

标签: c# android android-fragments xamarin

我的应用程序的流程如下:

MainActivity: HomeFragment - > BlackoutSetupFragment - > BlackoutFragment - > HomeFragment

由于某种原因,当我尝试从BlackoutFragment转到HomeFragment时,MainActivity变为空,直到我离开应用程序并返回它之前,什么都没有呈现。左侧是HomeFragment的通常外观,而右侧是我导航回首页时的外观:

该组我使用设置片段的方法:

    // Which is invoked like 
    // SetFragment(typeof(HomeFragment), "Home", true, true, false);
    public void SetFragment(Type fragmentType, string fragmentName, bool addToBackStack, bool showNavigationIndicator, bool useSlideAnimation)
    {
        if (fragmentType == typeof(HomeFragment))
            SetFragment(new HomeFragment(), fragmentType, fragmentName, addToBackStack, showNavigationIndicator, useSlideAnimation);
        else if (fragmentType == typeof(BlackoutFragment))
            SetFragment(new BlackoutFragment(), fragmentType, fragmentName, addToBackStack, showNavigationIndicator, useSlideAnimation);
        else if (fragmentType == typeof(BlackoutSetupFragment))
            SetFragment(new BlackoutSetupFragment(), fragmentType, fragmentName, addToBackStack, showNavigationIndicator, useSlideAnimation);
    }

    private void SetFragment(Fragment fragment, Type fragmentType, string fragmentName, bool addToBackStack, bool showNavigationIndicator, bool useSlideAnimation)
    {
        FragmentTransaction transaction = this.FragmentManager.BeginTransaction();

        if (useSlideAnimation)
            transaction.SetCustomAnimations(Resource.Animator.enter_from_left, Resource.Animator.exit_to_right, Resource.Animator.enter_from_right, Resource.Animator.exit_to_left);

        transaction.Replace(Resource.Id.content_main_fragment_layout, fragment, fragmentType.Name);

        if (addToBackStack)
            transaction.AddToBackStack(null);

        transaction.Commit();

        this.FragmentManager.ExecutePendingTransactions();

        if (!string.IsNullOrWhiteSpace(fragmentName))
            SetAppBarTitle(fragmentName);

        if (showNavigationIndicator) _navDrawerToggle.DrawerIndicatorEnabled = true;
        else _navDrawerToggle.DrawerIndicatorEnabled = false;
    }

和用于主作业的内容的axml是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">
    <FrameLayout
        android:id="@+id/content_main_fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

任何人都知道可能会发生在这里?我试图包括一切,我认为将是有益这里调试,但如果需要别的东西,拍我的注释。

2 个答案:

答案 0 :(得分:0)

您不应将初始Fragment添加到Backstack中,因为这将导致它是可逆事务。这意味着,如果您的初始状态为“无片段”,并且将“无片段”->“ HomeFragment”的事务添加到后台,那么您可以弹出回到“无片段”状态。

只需更改对初始Fragment的调用,就不会将其添加到后台。

答案 1 :(得分:0)

的问题,使用System.Timers.Timer,以便更新与所述BlackoutFragment一个倒计时的UI。用System.Threading.Timer代替似乎已解决了该问题。我的猜测是,在尝试从片段移开时,在UI线程上运行的事件“ RefreshView”正在引起某种时髦。

因此替换:

    public override void OnResume()
    {
        base.OnResume();
        //...
        _refreshTimer = new System.Timers.Timer();
        _refreshTimer.Interval = 1000;
        _refreshTimer.Elapsed += RefreshView;
        _refreshTimer.Start();
    }

    public override void OnPause() 
    {
        base.OnPause();
        //...
        _refreshTimer.Dispose();
    }

具有:

    public override void OnResume()
    {
        base.OnResume();
        //...
        _refreshTimer = new System.Threading.Timer(RefreshView, null, 0, 1000);
    }

    public override void OnPause()
    {
        base.OnPause();
        //...
        _refreshTimer.Dispose();
    }

解决了我的问题。