Bu dersimizde fragmentin çağrıldığı activity sınıfından fragmente nasıl veri gönderirirz onu anlatmaya çalıştım.
Video’da detaylı bir şekilde anlattığım gibi activitylerden fragmentlere veri göndermek için iki method mevcuttur.
1. method: Bundle kullanarak primitive tip dediğimiz int, boolean, float veri tiplerine sahip olan değişkenleri fragmentlere gönderebiliriz.
2.method: Fragment nesnesini direk olarak kullanarak primitive tiplerin yanı sıra, kendi oluşturduğumuz veri tiplerini ve hatta bunları tutan Arraylist gibi collection’ları da fragmente göndermemiz mümkün.
Örneğimizde activity’de bulunan iki tane edittextten aldığımız int değerleri fragmentimize gönderiyoruz ve onun içinde toplama işlemini yapıyoruz. Bunun için fragment’i activityimize eklerken bundle kullanarak iki tane int değeri ekleme işleminden önce fragmentimize gönderiyoruz. Bunu yapmak için fragmentA.setArguments methodunu kullandık.
Daha sonra fragment sınıfı içinde bu değerleri bundle’dan çıkarıp toplama işlemini gerçekleştiriyoruz. Bunun için fragment içinde getBundle methoduyla yeni bir bundle nesnesi oluşturuyoruz. Ve bu bundledan aldığımız degerleri degiskenlere getInt methoduyla atıyoruz.
Bu örneği bir de fragment nesnemizi direk olarak kullanarak gerçekleştirdik. Bunun için Activity’de oluşturduğumuz fragment nesnesi yardımıyla fragmentimiz içindeki bir methoda ulaşıyoruz. Sayı değerlerimizi bu methoda parametre olarak girerek activity’den fragmente veri alışverişini sağlamış oluyoruz. Bu yöntemi sadece primitive tip veriler için değil kendi yarattığımız veri tiplerini de gönderirken kullanabiliriz, ama Bundle ile veri gönderme methodunda kendi yarattığımız mesela Ogrenci veri tipindeki bir nesneyi gönderemeyiz.
Umarım faydalı bir ders olmuştur…
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package com.example.emrealtunbilek.activitydenfragmentedata; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.ButtonBarLayout; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { EditText sayi1; EditText sayi2; Button fragmenteGonder; FragmentManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sayi1= (EditText) findViewById(R.id.birinci_sayi); sayi2= (EditText) findViewById(R.id.ikinci_Sayi); fragmenteGonder= (Button) findViewById(R.id.btnFragmentegonder); manager=getFragmentManager(); } public void calistir(View v) { FragmentA fragmentA=new FragmentA(); int birinciSayi=Integer.parseInt(sayi1.getText().toString()); int ikinciSayi=Integer.valueOf(sayi2.getText().toString()) ; // Bundle bundle=new Bundle(); // // bundle.putInt("birinci", birinciSayi); // bundle.putInt("ikinci", ikinciSayi); fragmentA.sendData(birinciSayi, ikinciSayi); fragmentA.sendOgrenci(new Ogrenci()); FragmentTransaction transaction=manager.beginTransaction(); transaction.add(R.id.container, fragmentA,"fragA"); transaction.commit(); } class Ogrenci{ int ogrNo; String ad; } } |
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 48 49 50 51 52 53 54 55 56 57 |
package com.example.emrealtunbilek.activitydenfragmentedata; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.widget.ButtonBarLayout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; public class FragmentA extends Fragment { Button hesaplaButon; TextView sonucText; int sayi1=0, sayi2=0; MainActivity.Ogrenci ogr; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v= inflater.inflate(R.layout.fragment_a, container, false); /* Bundle bundle=getArguments(); final int sayi1=bundle.getInt("birinci", 0); final int sayi2=bundle.getInt("ikinci", 0); */ hesaplaButon= (Button) v.findViewById(R.id.btnHesapla); sonucText = (TextView) v.findViewById(R.id.tvSonuc); hesaplaButon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int sonuc=sayi1+sayi2; sonucText.setText("SONUC =" + sonuc); } }); return v; } public void sendData(int birinciSayi, int ikinciSayi) { this.sayi1=birinciSayi; this.sayi2=ikinciSayi; } public void sendOgrenci(MainActivity.Ogrenci ogrenci) { this.ogr=ogrenci; } } |
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 48 49 50 51 52 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.emrealtunbilek.activitydenfragmentedata.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@android:color/white" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/birinci_sayi" android:text="0" android:inputType="number" android:layout_weight="1" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ikinci_Sayi" android:inputType="number" android:text="0" android:layout_weight="1" /> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnFragmentegonder" android:text="FRAGMENT A'YA GÖNDER" android:onClick="calistir" /> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/container" android:layout_margin="10dp" > </FrameLayout> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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="#0fe" android:orientation="vertical"> <Button android:id="@+id/btnHesapla" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="HESAPLA" /> <TextView android:id="@+id/tvSonuc" android:layout_width="wrap_content" android:layout_gravity="center" android:layout_height="wrap_content" android:text="SONUÇ" /> </LinearLayout> |
Merhaba Emre Hocam,
TabLayout dersinizi izleyerek bir uygulama yaptım ancak bir problemim var,
Tablayout,view pager ve fragment kullanıyorum dersteki gibi, açılan activity içinde tablayout çalışıyor ve ben activityden açık olan fragment e veri göndermek istiyorum ne yaptıysam beceremedim.
Yardımınızı rica ediyorum.