Torna indietro   Hardware Upgrade Forum > Software > Programmazione

Wind Tre 'accende' il 5G Standalone in Italia: si apre una nuova era basata sui servizi
Wind Tre 'accende' il 5G Standalone in Italia: si apre una nuova era basata sui servizi
Con la prima rete 5G Standalone attiva in Italia, WINDTRE compie un passo decisivo verso un modello di connettività intelligente che abilita scenari avanzati per imprese e pubbliche amministrazioni, trasformando la rete da infrastruttura a piattaforma per servizi a valore aggiunto
OPPO Find X9 Pro: il camera phone con teleobiettivo da 200MP e batteria da 7500 mAh
OPPO Find X9 Pro: il camera phone con teleobiettivo da 200MP e batteria da 7500 mAh
OPPO Find X9 Pro punta a diventare uno dei riferimenti assoluti nel segmento dei camera phone di fascia alta. Con un teleobiettivo Hasselblad da 200 MP, una batteria al silicio-carbonio da 7500 mAh e un display da 6,78 pollici con cornici ultra ridotte, il nuovo flagship non teme confronti con la concorrenza, e non solo nel comparto fotografico mobile. La dotazione tecnica include il processore MediaTek Dimensity 9500, certificazione IP69 e un sistema di ricarica rapida a 80W
DJI Romo, il robot aspirapolvere tutto trasparente
DJI Romo, il robot aspirapolvere tutto trasparente
Anche DJI entra nel panorama delle aziende che propongono una soluzione per la pulizia di casa, facendo leva sulla propria esperienza legata alla mappatura degli ambienti e all'evitamento di ostacoli maturata nel mondo dei droni. Romo è un robot preciso ed efficace, dal design decisamente originale e unico ma che richiede per questo un costo d'acquisto molto elevato
Tutti gli articoli Tutte le news

Vai al Forum
Rispondi
 
Strumenti
Old 14-02-2014, 09:14   #1
Link_88
Member
 
Iscritto dal: May 2008
Messaggi: 45
[Android] Anomalia durante la modifica manuale del savedInstanceState di un ViewPager

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

Codice:
	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

Codice:
        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:

Codice:
        @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

Codice:
    @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:

Quote:
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?
Link_88 è offline   Rispondi citando il messaggio o parte di esso
 Rispondi


Wind Tre 'accende' il 5G Standalone in Italia: si apre una nuova era basata sui servizi Wind Tre 'accende' il 5G Standalone in Italia: s...
OPPO Find X9 Pro: il camera phone con teleobiettivo da 200MP e batteria da 7500 mAh OPPO Find X9 Pro: il camera phone con teleobiett...
DJI Romo, il robot aspirapolvere tutto trasparente DJI Romo, il robot aspirapolvere tutto trasparen...
DJI Osmo Nano: la piccola fotocamera alla prova sul campo DJI Osmo Nano: la piccola fotocamera alla prova ...
FUJIFILM X-T30 III, la nuova mirrorless compatta FUJIFILM X-T30 III, la nuova mirrorless compatta
Il Galaxy S26 Edge potrebbe essere ancor...
Google riaccenderà una centrale n...
Crollo per Pornhub nel Regno Unito:-77% ...
La Germania accende il suo cannone laser...
Il meglio di Amazon in 2 minuti: tira ar...
ECOVACS risponde a Eureka e dimezza il p...
Durissimo colpo per Nintendo: l'ufficio ...
Scope elettriche al minimo storico su Am...
Blue Jay e Project Eluna: robotica e AI ...
Scede a 949€ il Samsung Galaxy S25 Ultra...
Blue Yeti Nano in super offerta su Amazo...
Netflix sta preparando un'offerta per Wa...
Prezzo impossibile, è sceso ancor...
Torna il migliore dei mini PC economici:...
USA, via libera all'uso di plutonio mili...
Chromium
GPU-Z
OCCT
LibreOffice Portable
Opera One Portable
Opera One 106
CCleaner Portable
CCleaner Standard
Cpu-Z
Driver NVIDIA GeForce 546.65 WHQL
SmartFTP
Trillian
Google Chrome Portable
Google Chrome 120
VirtualBox
Tutti gli articoli Tutte le news Tutti i download

Strumenti

Regole
Non Puoi aprire nuove discussioni
Non Puoi rispondere ai messaggi
Non Puoi allegare file
Non Puoi modificare i tuoi messaggi

Il codice vB è On
Le Faccine sono On
Il codice [IMG] è On
Il codice HTML è Off
Vai al Forum


Tutti gli orari sono GMT +1. Ora sono le: 14:28.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Served by www3v