Wiederherstellung der fragment-Zustand beim ändern der Fragmente durch die Navigationsleiste unten

Habe ich unteren Navigationsleiste klicken Sie auf der Artikel in der Navigationsleiste bin ich einbauen Fragmente. Ich habe 3 Fragmente A,B,C usw. auf der b Element B-fragment geladen wird, und in B, ich rufe 3-4 APIs. Also wenn ich jetzt gehen Sie zu C und dann wieder kommen, um B eine neue Instanz von B-Fragment wird erstellt und erneut die APIs aufgerufen werden, wie kann ich speichern Sie das fragment-Instanz Staat und nicht APIs aufrufen, die wieder beim wechseln der Fragmente. Das ist mein code.

mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id = item.getItemId();
            Fragment currentLoaded = fgMan.findFragmentById(R.id.container_body);
            switch (id) {
                case R.id.nearby_fragment:
                    if (!(currentLoaded instanceof SpotFeedMapFragment)) {
                        removeScroll();
                        mNearByFragment = fgMan.findFragmentByTag(NEARBY_FRAGMENT_TAG) != null ? fgMan.findFragmentByTag(NEARBY_FRAGMENT_TAG) : mNearByFragment;
                        fgMan.beginTransaction().setCustomAnimations(R.anim.abc_fade_in, R.anim.abc_fade_out);
                        fgMan.beginTransaction().replace(R.id.container_body, mNearByFragment, NEARBY_FRAGMENT_TAG).commit();
                        fgMan.executePendingTransactions();
                        getSupportActionBar().setTitle(getString(R.string.nearby_fragment));
                    }
                    break;
                case R.id.route_fragment:
                    if (!(currentLoaded instanceof BusLocationsFragment)) {
                        if (!inParent) {
                            mRl.removeView(fixLayout);
                            p.addRule(RelativeLayout.BELOW, toolbar.getId());
                            scrollView.setLayoutParams(p);
                            scrollView.addView(fixLayout);
                            mRl.addView(scrollView);
                            inParent = true;
                        }
                        //mFragment = new BusLocationsFragment();
                        mBusLocFragment = fgMan.findFragmentByTag(BUS_LOC_FRAGMENT_TAG) != null ? fgMan.findFragmentByTag(BUS_LOC_FRAGMENT_TAG) : mBusLocFragment;
                        fgMan.beginTransaction().setCustomAnimations(R.anim.abc_fade_in, R.anim.abc_fade_out);
                        fgMan.beginTransaction().replace(R.id.container_body, mBusLocFragment, BUS_LOC_FRAGMENT_TAG).commit();
                        fgMan.executePendingTransactions();
                        getSupportActionBar().setTitle(getString(R.string.app_name));
                    }
                    break;
                case R.id.newsfeed_activity:
                    if (!(currentLoaded instanceof NewsFeedActivity)) {
                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
                            removeScroll();
                        }
                        mNewsFeedFragment = fgMan.findFragmentByTag(NEWSFEED_FRAGMENT_TAG) != null ? fgMan.findFragmentByTag(NEWSFEED_FRAGMENT_TAG) : mNewsFeedFragment;
                        fgMan.beginTransaction().setCustomAnimations(R.anim.abc_fade_in, R.anim.abc_fade_out);
                        fgMan.beginTransaction().replace(R.id.container_body, mNewsFeedFragment, NEWSFEED_FRAGMENT_TAG).commit();
                        fgMan.executePendingTransactions();
                        getSupportActionBar().setTitle(getString(R.string.news));
                    }
                    break;
            }
            return true;
        }
    });

Habe ich bereits initialisiert Fragmente member-Variablen oben in onCreatevon MainActivity

  • nur eine Idee kann ich nicht liefern-code, da ich nicht noch die dev tools jetzt. dont verwenden Sie .replace im fragment Transaktion. dadurch wird das zerstören der Instanz auf dem backstack. ersetzen Sie es mit .add. Wenn Sie auf eine Schaltfläche zu ändern, zum Beispiel fragment a nach b und dann fragment b ist bereits geöffnet, bevor, Sie können überprüfen Sie backstack, wenn das fragment b ist schon da-und pop-hier ist ein Beispiel stackoverflow.com/a/9787891/5870896 . Stellen Sie sicher, dass Sie das fragment auf backstack jedes mal, wenn Sie besuchen Sie es zu speichern, Beispiel
  • kennt das jemand, ist es besser, hide/show-fragment Transaktion statt trennen, entfernen, ersetzen oder hinzufügen? (wie dieser eine andere Antwort stackoverflow.com/q/42434392/4074312)
InformationsquelleAutor user007 | 2017-03-14
Schreibe einen Kommentar