4 Mayıs 2020 Pazartesi

Activity Sınıfı - Bir Tane Olmalı

Giriş
Bu sınıf Context sınıfından kalıtır. Activity pencere demek gibi düşünülmeli. Her activity xml dosyasına tanımlanır.
<activity android:name=".OrderScreen" />
Tek Bir Activity Sınıfı
Son zamandarda Bir Activity + N Tane Fragment kullanımı tavsiye ediliyor. Açıklaması şöyle.
You should try One activity and many fragments approach using Android Navigation Component
findViewById metodu
Bir actvitiy kendi içindeki görsel bileşenlere erişmek için findViewById metodunu kullanır. Bu metodu kullanıbilmek için önce setContentView() çağrısının yapılmış olması gerekir. Şöyle yaparız.
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ...
}
Örnek
Bu metoda her zaman R.id.xxx şeklide bir sabit geçilir. Şu kullanım şekli yanlıştır.
timePicker = (TimePicker) findViewById(R.layout.timePicker);
Örnek
Android O'dan önce findViewById metodu generic olmadığı için döndürülen nesneyi cast etmek gerekiyordu. Bu işten kaçmak için şöyle kodlar kullanılıyordu.
protected <T extends View> T $(@IdRes int id) {
  return (T) super.findViewById(id);
}
Bu kodu kullanmak için şöyle yaparız.
$(R.id.t9_key_0).setOnClickListener(this);
Örnek
Şöyle yaparız.
submit = (Button) findViewById(R.id.submit);
empId_edt = (EditText) findViewById(R.id.emp_id);
finish metodu
Pencereyi kapatır. Şöyle yaparız.
activity.finish();
getWindow metodu
Activity içinde bulunduğu pencereye getWindow() ile erişir. Pencerenin arka planına resim eklemek istersek şöyle yaparız.
getActivity().getWindow().setBackgroundDrawableResource(yourBackgroundid);
onActivityResult metodu
startActivityForResult metodu yazına taşıdım.

onCreate metodu
Yaşam döngüsünü buradaki şekilde görmek mümkün. Açıklaması şöyle
onCreate() counter part is onDestroy()
onStart() counter part is onStop()
onPause() counter part is onResume()
Ekran ilk yaratılırken çağrılır. Standart kullanım şöyle. super ile ata sınıfın metodu çağrılır. setContentView ile ekran bileşenleri yaratılır.
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ... 
}
Açıklaması şöyle.
When screen rotates, Android restarts the running Activity (onDestroy() is called, followed by onCreate()). The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.
onDestroy metodu
Açıklaması şöyle.
When we open Task manager and swipe the app left/right , onDestroy() is called
Açıklaması şöyle.
Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.
onDestroy() killable bir metod. Yani koşmayabilir. Açıklaması şöyle.
for those methods that are marked as being killable, after that method returns the process hosting the activity may be killed by the system at any time without another line of its code being executed. Because of this, you should use the onPause() method to write any persistent data (such as user edits) to storage.
Şöyle yaparız.
@Override
protected void onDestroy() {
  ...  
  super.onDestroy();
}
onInterceptTouchEvent metodu
Açıklaması şöyle.
Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.
Ancak bu metod dikkatli kullanılmalı. Açıklaması şöyle.
Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it requires implementing that method as well as this one in the correct way.

onOptionsItemSelected metodu
Açıklaması şöyle.
Return false to allow normal menu processing to proceed, true to consume it here.
Şöyle yaparız.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
  int id = item.getItemId();
  if(id == R.id.nav_search) {
    i = new Intent(this, SearchActivity.class);
    startActivity(i);
    return true;
  }

  return super.onOptionsItemSelected(item);
}
onPause metodu
Kullanıcı ekranı kapatma tuşuna basarsa çağrılır.

onRestart metodu
Kullanıcı ekranı açarsa çağrılır.

onResume metodu
Pencere tekrar görünür hale gelince çağrılır. Metodun imzası şöyledir.
public void onResume(){
// Block of code
}
onStop metodu
Temizlik yapan kodlar buraya yazılır.

requestWindowFeature metodu
Her activity bir feature kümesine sahiptir. Feature kümesi pencerenin nasıl çizilmesi gerektiğini belirtir. Feature kümesi Toolbar, actionbar, icon çiziminin nasıl olacağı gibi şeylerdir.
Bazı özellikler değiştirilebilir. Örneğin pencerede başlık olması isteriz. Bu durumda şöyle yaparız.
requestWindowFeature(Window.FEATURE_NO_TITLE);
startActivityForResult metodu
startActivityForResult metodu yazına taşıdım.

Hiç yorum yok:

Yorum Gönder