Fragment Transaction加载一个空白视图



问题似乎是,在罕见的情况下,碎片似乎以一种非常奇怪的方式加载。

有时片段的视图不可见,调用onCreateView,但它不会将视图添加到活动中,只是加载一个空白的白色页面。当从恢复应用程序调用onResume时(例如转到最近的应用程序并单击我的应用程序),会显示该视图。

显然,所有的碎片事务都是在MAIN线程上完成的。

这种情况大约每20次发生1次。

我很乐意提供您想要的任何代码。所以,如果有什么东西不见了,请告诉我,我会很乐意为你提供

我的活动中的onCreate:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Variables.context = this;
        Variables.activity = this;
        Variables.p_class = HomeActivity.class;
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        }
        toolbar.setNavigationIcon(null);
        toolbar.getMenu().clear();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        actionbar_pw_spinner = (ProgressWheel) findViewById(R.id.actionbar_pw_spinner);
        actionbar_pw_mins = (TextView) findViewById(R.id.actionbar_pw_mins);
        actionBarTitleTV = (TextView) findViewById(R.id.custom_actionbar_title);
        usersName = (TextView) findViewById(R.id.usersName);
        usersEmail = (TextView) findViewById(R.id.usersEmail);
        actionBarTitleTV.setText(this.getTitle());
        btnBack = (ImageView) findViewById(R.id.button_back);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        btnBack.setVisibility(View.GONE);
        btnMenu = (ImageView) findViewById(R.id.button_menu);
        btnMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openDrawer();
            }
        });
        btnMenu.setVisibility(View.GONE);
        setPersonInfo();
        Fabric.with(this, new Crashlytics());
        Variables.myLocation = new Location("myLoc");
        Variables.myLocation.setLatitude(0.0);
        Variables.myLocation.setLongitude(0.0);
        if (!Variables.isThisBidTaxi()) {
            CustomerComms.myOwnSocket = new MySocket(null);
            CustomerComms.myOwnSocket.start();
        }
        SettingSQLHelper helper = new SettingSQLHelper(HomeActivity.this);
        Variables.setting = helper.getSetting();
        helper.close();
        mRequestingLocationUpdates = false;
        buildGoogleApiClient();
        createLocationRequest();
        buildLocationSettingsRequest();
        checkLocationSettings();
        setTitle("");
        if (savedInstanceState != null) {
            return;
        } else {
            getSupportFragmentManager().beginTransaction().add(R.id.fragmentspace, new SplashFragment()).commitAllowingStateLoss();
            ((HomeActivity) Variables.context).getSupportFragmentManager().executePendingTransactions();
        }
        containLayout = null;
        containLayout = (FrameLayout) findViewById(R.id.fragmentspace);
        getSupportFragmentManager().addOnBackStackChangedListener(getListener());
        m_updateBooking = (Booking) getIntent().getSerializableExtra("booking");
        m_TrackDriver = (Boolean) getIntent().getSerializableExtra("track");
        Variables.initBooking();
    }

我的onResume在我的活动中:

 protected void onResume() {
        super.onResume();
        if (mGoogleApiClient.isConnected() && mRequestingLocationUpdates) {
            startLocationUpdates();
        }
        Variables.context = this;
        Variables.activity = this;
        if (m_updateBooking != null) {
            if (m_updateBooking.getJobStatus() == Booking.eeJob_Complete) {
                if (!Variables.driverrated) {
                    DriverRatingFragment DriverRatingFRA = DriverRatingFragment.newInstance(m_updateBooking);
                    getSupportFragmentManager().beginTransaction().add(R.id.fragmentspace, DriverRatingFRA).addToBackStack(DriverRatingFragment.class.getName()).commitAllowingStateLoss();
                    ((HomeActivity) Variables.context).getSupportFragmentManager().executePendingTransactions();
                }
            } else {
                if (m_TrackDriver) {
                    getSupportFragmentManager().beginTransaction().add(R.id.fragmentspace, TrackingDriverFragment.newInstance(m_updateBooking)).addToBackStack(TrackingDriverFragment.class.getName()).commitAllowingStateLoss();
                    ((HomeActivity) Variables.context).getSupportFragmentManager().executePendingTransactions();
                } else {
                    gotoMyBooking(m_updateBooking);
                }
            }
        }
        m_updateBooking = null;
        getIntent().removeExtra("booking");
        getIntent().removeExtra("track");
        if (Variables.setting == null) {
            final SettingSQLHelper helper = new SettingSQLHelper(HomeActivity.this);
            Variables.setting = helper.getSetting();
        }
    }

我的onPause

  @Override
    protected void onPause() {
        super.onPause();
        if (mGoogleApiClient.isConnected()) {
            stopLocationUpdates();
        }
    }
My onCreateView inside a fragment looks like this:
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
           inflatedView = inflater.inflate(R.layout.fragment_new_booking, container, false);//the rest of my code then follows here
    return inflaterview
        }

我的活动XML文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ProgressWheel="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".Activity.HomeActivity">
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay">
                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true">
                    <TextView
                        android:id="@+id/custom_actionbar_title"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:singleLine="true"
                        android:text="@string/new_booking"
                        android:textColor="@color/White"
                        android:textSize="18sp" />
                    <FrameLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical">
                        <ImageView
                            android:id="@+id/button_back"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_gravity="center_vertical|start"
                            android:contentDescription="@string/back_button"
                            android:fitsSystemWindows="true"
                            android:src="@drawable/ic_arrow_back_white_24dp" />
                        <ImageView
                            android:id="@+id/button_menu"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_gravity="center_vertical|start"
                            android:contentDescription="@string/menu_button"
                            android:fitsSystemWindows="true"
                            android:src="@drawable/ic_menu_white_24dp" />
                    </FrameLayout>
                    <com.todddavies.components.progressbar.ProgressWheel
                        android:id="@+id/actionbar_pw_spinner"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_gravity="top|end"
                        android:layout_marginRight="10dp"
                        android:visibility="gone"
                        ProgressWheel:barColor="#86f066"
                        ProgressWheel:barLength="10dp"
                        ProgressWheel:barWidth="1dp"
                        ProgressWheel:rimColor="@color/White"
                        ProgressWheel:rimWidth="2dp"
                        ProgressWheel:textColor="@color/White"
                        ProgressWheel:textSize="15sp" />
                    <TextView
                        android:id="@+id/actionbar_pw_mins"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:layout_marginRight="15dp"
                        android:layout_marginTop="27dp"
                        android:text="@string/mins"
                        android:textColor="@color/White"
                        android:textSize="10sp"
                        android:visibility="gone" />
                </FrameLayout>
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.AppBarLayout>
        <FrameLayout
            android:id="@+id/fragmentspace"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/White" />
    </android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

我的Android清单如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.commandsoftware.androidbookingapp"
    android:installLocation="auto">
    <uses-permission android:name="com.chasesoftware.cabmaster.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">>
        <activity
            android:name=".Activity.HomeActivity"
            android:label="@string/app_name"
            android:noHistory="false"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_api" />
        <receiver android:name=".Activity.SMSReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
        <meta-data
            android:name="io.fabric.ApiKey"
            android:value="@string/fabric_key" />
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />
        <provider
            android:name="com.facebook.FacebookContentProvider"
            android:authorities="@string/facebook_api"
            android:exported="false" />
        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    </application>
</manifest>

当我提交Fragment事务时,我的代码如下:

((HomeActivity) Variables.context).getSupportFragmentManager().beginTransaction().remove(((HomeActivity) Variables.context).getSupportFragmentManager().findFragmentById(R.id.fragmentspace)).commitAllowingStateLoss();
            ((HomeActivity) Variables.context).getSupportFragmentManager().executePendingTransactions();
            ((HomeActivity) Variables.context).getSupportFragmentManager().beginTransaction().add(R.id.fragmentspace, new MyBidsFragment().newInstance(booking)).addToBackStack(MyBidsFragment.class.getName()).commitAllowingStateLoss();
            ((HomeActivity) Variables.context).getSupportFragmentManager().executePendingTransactions();

碎片交易的另一个例子

 myBookingsFragment = MyBookingsFragment.newInstance(updateBooking);
        getSupportFragmentManager().beginTransaction().add(R.id.fragmentspace, myBookingsFragment).addToBackStack(myBookingsFragment.getClass().getName()).commitAllowingStateLoss();
        getSupportFragmentManager().executePendingTransactions();

我从开始

  • 替换:

      ((HomeActivity) Variables.context).getSupportFragmentManager().beginTransaction().remove(((HomeActivity) Variables.context).getSupportFragmentManager().findFragmentById(R.id.fragmentspace)).commitAllowingStateLoss();
                ((HomeActivity) Variables.context).getSupportFragmentManager().executePendingTransactions();
                ((HomeActivity) Variables.context).getSupportFragmentManager().beginTransaction().add(R.id.fragmentspace, new MyBidsFragment().newInstance(booking)).addToBackStack(MyBidsFragment.class.getName()).commitAllowingStateLoss();
                ((HomeActivity) Variables.context).getSupportFragmentManager().executePendingTransactions();
    

    带有

        ((HomeActivity) getActivity()).getSupportFragmentManager().
              beginTransaction().
              replace(R.id.fragmentspace, new MyBidsFragment().newInstance(booking)).
              commit();
    

    (顺便说一句,让newInstance()成为static,这样你就不会对MyBidsFragment的新实例进行这种奇怪的初始化)

  • 然后更换

     myBookingsFragment = MyBookingsFragment.newInstance(updateBooking);
            getSupportFragmentManager().beginTransaction().add(R.id.fragmentspace, myBookingsFragment).addToBackStack(myBookingsFragment.getClass().getName()).commitAllowingStateLoss();
            getSupportFragmentManager().executePendingTransactions();
    

    带有

     myBookingsFragment = MyBookingsFragment.newInstance(updateBooking);
     getSupportFragmentManager().beginTransaction().
               replace(R.id.fragmentspace, myBookingsFragment).
               addToBackStack(myBookingsFragment.getClass().getName()).
               commit();
    

    replace(),而不是add()

  • 然后我会对"活动"有1000行代码这一事实做点什么。它使代码更难维护和理解(甚至在SO上发布)

我希望它能帮助

最新更新