CERCA
PER MODELLO
FullScreen Chatbox! :)

Utente del giorno: bluemask con ben 5 Thanks ricevuti nelle ultime 24 ore
Utente della settimana: bluemask con ben 4 Thanks ricevuti negli ultimi sette giorni
Utente del mese: megthebest con ben 28 Thanks ricevuti nell'ultimo mese

Visualizzazione dei risultati da 1 a 2 su 2
Discussione:

Anomalia durante la modifica manuale del savedInstanceState di un ViewPager Fragment

Se questa discussione ti è stata utile, ti preghiamo di lasciare un messaggio di feedback in modo che possa essere preziosa in futuro anche per altri utenti come te!
  1. #1
    Baby Droid


    Registrato dal
    Feb 2011
    Messaggi
    12

    Ringraziamenti
    0
    Ringraziato 0 volte in 0 Posts
    Predefinito

    Anomalia durante la modifica manuale del savedInstanceState di un ViewPager Fragment

    Salve a tutti, ho aperto una discussione su stackoverflow ma non ho ricevuto risposta.

    Incollo qui la mia domanda che ho fatto lì.

    Se ci sono problemi con il fatto che il seguente testo è in inglese vi prego di farmelo sapere, lo traduco senza alcun problema!

    I have a ViewPager (instantiated with FragmentStatePagerAdapter) with some Fragment attached to it.

    In a specific usecase I need to reset instanceBean and UI for most of the fragments in the pager.

    After some googling I have tried some solutions like this but the side effects were not easy manageable. Other solution like this doesn't match my needs.

    So I decided to go straight with the manual reset of the UI and instanceBean obj like in the code below:

    The code

    Single fragment reset

     
    1
        public void initFragment() {
    2
            notaBean = new NoteFragmentTO();
    3
            
    4
            fromSpinnerListener = false;
    5
        }
    6
        
    7
        public void resetFragment() {
    8
            initFragment();
    9
            NoteFragment.retainInstanceState = false;
    10
        }


    This is done with the following code from the parent Activity:

    Fragment reset from parent

    13
     
    1
            private void resetAfterSaving() {
    2
            mIndicator.setCurrentItem(POSITION_F*****);
    3
            f*****Info.resetFragment();
    4
            
    5
            mIndicator.setCurrentItem(POSITION_NOTE);
    6
            noteInfo.resetFragment();
    7
            
    8
            mIndicator.setCurrentItem(POSITION_M*****);
    9
            m*****Info.resetFragment();
    10
            
    11
            mIndicator.setCurrentItem(POSITION_V*****);
    12
            v*****.resetFragment();
    13
        }


    AfterViews method:

    26
     
    1
            @AfterViews
    2
            public void afterView() {
    3
                if (mSavedInstanceState != null) {
    4
                    restoreState(mSavedInstanceState);
    5
                }
    6
        
    7
                NoteFragment.retainInstanceState = true;
    8
                
    9
                // Inits the adapters
    10
                noteAdapter = new NoteArrayAdapter(this, noteDefaultList);
    11
        
    12
                sp_viol_nota_default.setAdapter(noteAdapter);
    13
                //sp_viol_nota_default.seton
    14
                
    15
                et_viol_nota.setOnFocusChangeListener(new OnFocusChangeListener() {
    16
        
    17
                    @Override
    18
                    public void onFocusChange(View v, boolean hasFocus) {
    19
                        if (!hasFocus) {
    20
                            String readText = et_viol_nota.getText().toString().trim();
    21
                            
    22
                            notaBean.setNota(readText == "" ? null : readText);
    23
                        }
    24
                    }
    25
                });
    26
            }


    OnSavedInstanceState

    10
     
    1
        @Override
    2
        public void onSaveInstanceState(Bundle outState) {
    3
            super.onSaveInstanceState(outState);
    4
            
    5
            outState.putParcelableArrayList(KEY_NOTE_D_LIST, (ArrayList<VlzAnagraficaNoteagente>) noteDefaultList);
    6
            outState.putInt(KEY_NOTE_D_POSITION, !NoteFragment.retainInstanceState ? 0 : notePosition);
    7
        
    8
            notaBean.setNota(!NoteFragment.retainInstanceState ? "" : et_viol_nota.getText().toString().trim());
    9
            outState.putParcelable(NoteFragmentTO.INTENT_KEY, notaBean);
    10
        }


    ----------

    Why do I set every page before resetting them?

    Because like explained here:

    When pages are not visible to the user, their entire fragment may be destroyed, only keeping the saved state of that fragment.
    and because until I don't select the relative fragment the @AfterViews method (that is everything processed right after OnCreateView of the fragment) is not executed.
    This throws NullPointerException for a thousand of reason (Usually in the @AfterViews method You launch RestoreState method, initializes adapter, do UI stuff).

    Setting the relative page before the reset let @AfterViews method be processed.


    ----------


    Before checking what would happened when rotating the device, all the fragment I need are correcly reset.

    When rotating the device, the error comes out:

    The views (mainly EditText) go back to their previous state BEFORE my reset.


    What happens?

    When switching between the page, at a certain point the page will be destroyed and OnSavedInstanceState is called everytime for each page.

    I have already handled the OnSavedInstanceState (like above) that when the boolean is false saves the state like if it had just been created.

    I found that until within AfterView method the EditText has its text set to blank (like I want) but going on with the debug the EditText goes back to its previous state, so at the end it will show the last text it had.

    Question

    How can I keep the manually set (in OnSavedInstanceState) EditText text after destroying/recreating a fragment?

  2.  
  3. #2
    Baby Droid


    Registrato dal
    Feb 2011
    Messaggi
    12

    Ringraziamenti
    0
    Ringraziato 0 volte in 0 Posts
    Predefinito

    Ci sono news su questo mio problema?

Tag per questa discussione

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire risposte
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Torna su
Privacy Policy