PDA

View Full Version : [Android] Anomalia durante la modifica manuale del savedInstanceState di un ViewPager


Link_88
14-02-2014, 08:14
Salve a tutti, ho aperto una discussione su stackoverflow (http://stackoverflow.com/questions/21625962/how-to-preserve-manually-set-instancestate-of-viewpager-fragments-in-depth-expl) 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 (http://stackoverflow.com/questions/16560485/remove-all-fragments-from-viewpager-pupolated-by-fragmentstatepageradapter) but the side effects were not easy manageable. Other solution like this (http://stackoverflow.com/questions/10396321/remove-fragment-page-from-viewpager-in-android) 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

public void initFragment() {
notaBean = new NoteFragmentTO();

fromSpinnerListener = false;
}

public void resetFragment() {
initFragment();
NoteFragment.retainInstanceState = false;
}

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

Fragment reset from parent

private void resetAfterSaving() {
mIndicator.setCurrentItem(POSITION_F*****);
f*****Info.resetFragment();

mIndicator.setCurrentItem(POSITION_NOTE);
noteInfo.resetFragment();

mIndicator.setCurrentItem(POSITION_M*****);
m*****Info.resetFragment();

mIndicator.setCurrentItem(POSITION_V*****);
v*****.resetFragment();
}

AfterViews method:

@AfterViews
public void afterView() {
if (mSavedInstanceState != null) {
restoreState(mSavedInstanceState);
}

NoteFragment.retainInstanceState = true;

// Inits the adapters
noteAdapter = new NoteArrayAdapter(this, noteDefaultList);

sp_viol_nota_default.setAdapter(noteAdapter);
//sp_viol_nota_default.seton

et_viol_nota.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
String readText = et_viol_nota.getText().toString().trim();

notaBean.setNota(readText == "" ? null : readText);
}
}
});
}

OnSavedInstanceState

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putParcelableArrayList(KEY_NOTE_D_LIST, (ArrayList<VlzAnagraficaNoteagente>) noteDefaultList);
outState.putInt(KEY_NOTE_D_POSITION, !NoteFragment.retainInstanceState ? 0 : notePosition);

notaBean.setNota(!NoteFragment.retainInstanceState ? "" : et_viol_nota.getText().toString().trim());
outState.putParcelable(NoteFragmentTO.INTENT_KEY, notaBean);
}

----------

Why do I set every page before resetting them?

Because like explained here (http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html):

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?