Bu derste fragmentlerde screen orientation yani ekran değişimlerinde uygulamamızın nasıl davrandığından ve veri kaybını önlemek için onsaveinstance methodunun nasıl kullanıldığından bahsetmeye çalıştım.
Ekran değişimleri durumlarında activity’miz destroy edilir ve tekrar oluşturulur. Bu durumda bazı view öğeleri değerlerini korurken bazıları bu değerleri koruyamaz. Örneğin edittext, radiabutton, checkbox ve togglebutton değerlerini korurken button ve textviewin değerleri korunmaz. Bu veri kaybını önlemek için onSaveInstance ile ekran değişimi öncesinde verileri bundle ile korumak yani kaydetmek gerekir.
Daha sonra bu değerler onCreate methodunda bundle’dan çıkarılıp eski yerlerine konulur.
Örnek uygulamanın kodları aşağıdaki gibidir.
Umarım faydalı bir ders olmuştur…
1 2 3 4 5 6 7 8 9 |
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.emrealtunbilek.fragmentscreenorientation.MainActivity"> <fragment android:layout_width="wrap_content" android:layout_height="wrap_content" android:name="com.example.emrealtunbilek.fragmentscreenorientation.FragmentA" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:id="@+id/fragment" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" tools:layout="@layout/fragment_a" /> </RelativeLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0ec" android:orientation="vertical"> <TextView android:text="TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvDegistir" android:textAppearance="@style/TextAppearance.AppCompat.Title" /> <Button android:text="Button" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnGiris" /> <ToggleButton android:text="ToggleButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/toggleButton" /> <CheckBox android:text="CheckBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/checkBox" /> <RadioButton android:text="RadioButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/radioButton" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPersonName" android:text="Name" android:ems="10" android:id="@+id/editText" /> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
public class FragmentA extends Fragment { TextView text; Button buton; int sayi; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_a, container, false); sayi=0; text= (TextView) v.findViewById(R.id.tvDegistir); buton= (Button) v.findViewById(R.id.btnGiris); if(savedInstanceState!=null) { text.setText(savedInstanceState.getString("tvIcerik")); buton.setText(savedInstanceState.getString("btnIcerik")); } buton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { text.setText("BUTONA TIKLADINIZ"); buton.setText("TIKLANDI"); sayi=50; Toast.makeText(getActivity(), "sayı ="+sayi, Toast.LENGTH_LONG).show(); } }); return v; } @Override public void onSaveInstanceState(Bundle outState) { outState.putString("tvIcerik", text.getText().toString()); outState.putString("btnIcerik", buton.getText().toString()); super.onSaveInstanceState(outState); } } |