» »

[Android] onSaveInstanceState Problem

[Android] onSaveInstanceState Problem

shadeX ::

Imam preference activity katerega ustvari eden izmed gumbov v activitju. Ko se activity zamenja ( se pravi iz current activitija gre v preference activity ) se kliče metoda onSaveInstanceState ( ta metoda se kliče zato , ker ko user pritisne "back" gumb na napravi mu pač naloži prejšni activity v taki obliki kot je bil. Ta metoda samo shrani primerek activitja ). Nato sem ustvaril TimePicker dialog, ki popolnoma deluje če ga povežem z gumbom (recimo če imam gumb v navadnem activitiju in ga nekdo pritisne se mu pokaže dialog v katerem user izbere določen čas. Ampak to ni primarna naloga timepicker dialoga. (tako sem se samo prepričal da TimePicker dialog res deluje ).

Tukaj imam TimePicker class, ki je minimalno sestavljen ampak kot že rečeno delujoč.

package com.example.timepicker2;
public class FragmentClass extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		
		return new TimePickerDialog(getActivity(), this, 15, 00, false);
	}

	@Override
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

		Log.i("APP", "hour: "+hourOfDay+", min: "+minute);
		
	}

}


Nato imam MainActivity v katerem imam samo gumb. Ko pritisnem na ta gumb se odpre preference activity (Naredil sem ga static inner class, zato ker samo tako lahko sem dobil primerek FragmentManagerja, ki pa je nujen za nastanek time picker dialoga )

Se pravi koda mojega celega razreda (outer in inner class)


package com.example.timepicker2;
public class MainActivity extends FragmentActivity {
	
	static FragmentManager fm;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		fm = getSupportFragmentManager();

		
		Button but = (Button) findViewById(R.id.button1);
		but.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				startActivity(new Intent(MainActivity.this, TestSettings.class));
				
				
				
			} 
		});
		
		
	} 
	
	public static class TestSettings extends PreferenceActivity implements Preference.OnPreferenceClickListener{
		
		TimePicker tp1;
		PreferenceManager pm;
	
		
		
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			addPreferencesFromResource(R.xml.pref);
		getPreferenceManager().findPreference("key").setOnPreferenceClickListener(this);
		
		}

		@Override
		public boolean onPreferenceClick(Preference preference) {

			FragmentClass fc = new FragmentClass();
			fc.show(fm, "TAG");
			

			return false;
		}

	}
	
}


Tukaj je pa še moj preference xml file:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    
    
    <CheckBoxPreference android:title="Checkbox" android:summary="Checkbox"/>
    <EditTextPreference android:summary="time" android:dialogTitle="Hello"  />
	<Preference android:title="Prefff" android:key="key"/>
</PreferenceScreen>



Se pravi scenarij je tak:

Kliknem na gumb v MainActivitju. Ta gumb brez problema odpre preference activity (vmes je klican onSaveInstanceState) in preference activity se lepo prikaže. Če kliknem na edittextpreference se dialog (ki je že vgrajen) odpre in zapre brez problema. Nato sem naredil OnPreferenceClickListener, ki seveda posluša na klike ki jih prejme preference z ključem "key". To deluje normalno, ker LOGCAT pokaže da je bil preference kliknjen. Toast tudi deluje brez problema.

Problem nastane ko želim OnPreferenceClick() metodi ustvariti NOV FragmentClass (time picker dialog) in klicati njegovo metodo show(). Takrat aplikacija se prisilno zapre in LOGCAT vrže exception:

03-21 10:06:41.325: E/AndroidRuntime(11003): FATAL EXCEPTION: main
03-21 10:06:41.325: E/AndroidRuntime(11003): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1327)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1338)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.DialogFragment.show(DialogFragment.java:127)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at com.example.timepicker2.MainActivity$TestSettings.onPreferenceClick(MainActivity.java:110)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.preference.Preference.performClick(Preference.java:951)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:215)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.widget.AbsListView$1.run(AbsListView.java:3423)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.os.Handler.handleCallback(Handler.java:725)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.os.Looper.loop(Looper.java:137)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.app.ActivityThread.main(ActivityThread.java:5041)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at java.lang.reflect.Method.invokeNative(Native Method)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at java.lang.reflect.Method.invoke(Method.java:511)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at dalvik.system.NativeStart.main(Native Method)


Zakaj mi ne dovoli prikazati TimePicker dialoga ko kliknem na preference v preference activitiju?
  • spremenil: shadeX ()

Mavrik ::

Verjetno si padel v tale bug v support paketu.

Rešitve bi naj bile:

1.) Da ne kličeš super v onSavedInstanceState() ali
2.) FragmentTransaction kličeš z commitAllowingStateLoss()

Probaj pa poročaj.
The truth is rarely pure and never simple.

shadeX ::

Poskusil tvojo prvo točko in ne deluje. Niti na API < 11, ker to naj bi bil bug od API Level 11+. Sedaj se sprašujem, ali je res tako zakomplicirano narediti en TimePicker dialog v Preference Activitiju?


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Programiranje problem android

Oddelek: Programiranje
51166 (931) g333kk
»

[Android] Aplikacija se zruši

Oddelek: Programiranje
6809 (646) g333kk
»

android črta

Oddelek: Programiranje
412392 (1663) g333kk
»

[android] vstavljanje slike

Oddelek: Programiranje
71233 (1130) messi
»

[android] crash

Oddelek: Programiranje
61330 (1139) messi

Več podobnih tem