AndoridのAnnotationフレームワークの初使用(四)


煩わしいfinViewByIdの代わりに
@EActivity
public class MyActivity extends Activity {
  // Injects R.id.myEditText
  @ViewById
  EditText myEditText;
  @ViewById(R.id.myTextView)
  TextView textView;
}

ビューのロードが完了してから実行する方法を指定します@AfterView
@EActivity(R.layout.main)
public class MyActivity extends Activity {
    @ViewById
    TextView myTextView;
    @AfterViews
    void updateTextWithDate() {
        myTextView.setText("Date: " + new Date());
    }
}

注意:viewに関するメソッドをonCreateに書かないでください.
@Extra転送用Intent
@EActivity
public class MyActivity extends Activity {
  @Extra("myStringExtra")
  String myMessage;
  @Extra("myDateExtra")
  Date myDateExtraWithDefaultValue = new Date();
   // The name of the extra will be "myMessage"
  @Extra
  String myMessage;
}

onNewIntentはIntentに基づいてExtraを再注入できる
@EActivity
public class MyActivity extends Activity {
    @Extra("myStringExtra")
    String myMessage;
    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
    }
}

 
@SystemServiceno more Context.getSystemService()
@EActivity
public class MyActivity extends Activity {
  @SystemService
  NotificationManager notificationManager;
}

@SharedPref:
定義:
@SharedPref
public interface MyPrefs {
        // The field name will have default value "John"
    @DefaultString("John")
    String name();
        // The field age will have default value 42
    @DefaultInt(42)
    int age();
        // The field lastUpdated will have default value 0
    long lastUpdated();
    @DefaultRes(R.string.defaultPrefName)
    String resourceName();

    @DefaultRes
    String defaultPrefAge();
}

次の操作を行います.
@EActivity
public class MyActivity extends Activity {
    @Pref
    MyPrefs_ myPrefs;
}

// Simple edit
myPrefs.name().put("John");

// Batch edit
myPrefs.edit()
  .name()
  .put("John")
  .age()
  .put(42)
  .apply();

// Preference clearing:
myPrefs.clear();

// Check if a value exists:
boolean nameExists = myPrefs.name().exists();

// Reading a value
long lastUpdated = myPrefs.lastUpdated().get();

// Reading a value and providing a fallback default value
long now = System.currentTimeMillis();
long lastUpdated = myPrefs.lastUpdated().getOr(now);