Torna indietro   Hardware Upgrade Forum > Software > Programmazione

Hyundai Ioniq 9: dopo due settimane di test non avremmo voluto restituirla
Hyundai Ioniq 9: dopo due settimane di test non avremmo voluto restituirla
Una ammiraglia elettrica, da sei o sette posti, con il comfort di viaggio come missione assoluta. Un'auto non per tutti, ma di cui ti innamori dopo pochi giorni, ecco com'è andata la nostra prova di lunga durata
LG UltraGear evo GM9: 27 pollici, 5K, Mini LED e Dual Mode
LG UltraGear evo GM9: 27 pollici, 5K, Mini LED e Dual Mode
Hyper Mini LED con 2.304 zone, DisplayPort 2.1 e Dual Mode: 5K a 165 Hz oppure QHD a 330 Hz: LG propone un monitor da gioco ma dalla buona versatilità creativa, con prestazioni interessanti e un listino di 1.220 euro. Peccato l'alimentatore ingombrante
Motorola edge 70 Fusion FIFA World Cup 26 Edition: un ottimo smartphone per i fan del calcio
Motorola edge 70 Fusion FIFA World Cup 26 Edition: un ottimo smartphone per i fan del calcio
Motorola edge 70 Fusion FIFA World Cup 26 Edition porta la maglia iridata su uno smartphone accessibile: display Extreme AMOLED da 6,78 pollici a 144Hz, fotocamera Sony LYTIA 710 con moto ai, Snapdragon 7s Gen 3 abbinato a 8GB di RAM, batteria da 5200 mAh con ricarica TurboPower a 68W e certificazione IP68/IP69, il tutto in una veste PANTONE Silhouette con logo dorato a 24 carati, moto buds bass incluse in confezione e contenuti software dedicati ai Mondiali 2026
Tutti gli articoli Tutte le news

Vai al Forum
Rispondi
 
Strumenti
Old 14-02-2014, 08: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


Hyundai Ioniq 9: dopo due settimane di test non avremmo voluto restituirla Hyundai Ioniq 9: dopo due settimane di test non ...
LG UltraGear evo GM9: 27 pollici, 5K, Mini LED e Dual Mode LG UltraGear evo GM9: 27 pollici, 5K, Mini LED e...
Motorola edge 70 Fusion FIFA World Cup 26 Edition: un ottimo smartphone per i fan del calcio Motorola edge 70 Fusion FIFA World Cup 26 Editio...
Renault 5 elettrica al giudizio definitivo: solo stupore iniziale o premi meritati? Renault 5 elettrica al giudizio definitivo: solo...
HONOR CHOICE Projector Air Pro: piccolo, portatile e con Netflix HONOR CHOICE Projector Air Pro: piccolo, portati...
Ristrutturazione in casa EA: licenziamen...
Cloudflare avverte: gli umani rischiano ...
Amazon ha costruito un data center da 2 ...
Smartphone premium oltre i 600$: Apple d...
La RAM costa come nel 2007: 20 anni di r...
Huawei presenta MateBook Pro S: laptop u...
Pieghevoli, la svolta che tutti aspettav...
Sempre più manager tech sognano di molla...
IA agentica: Qwen3.8 Max davanti ai big ...
Trump deciso a bloccare l'eolico offshor...
TOP 12 offerte del weekend Amazon: i nuo...
Enhanced PSSR sarà attivo per imp...
Polti Vaporetto SV440 Double, la scopa a...
Smart TV a 129€ su Amazon: è un H...
Proseguono le operazioni di ripristino d...
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: 06:11.


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