Share
Embed
AndroidAnnotations Framework - Love it or hate it
An AndroidAnnotations framework intro. Pros and cons.
Andrei Verdes
April 23, 2016
Transcript
86 slides
-
AndroidAnnotations Framework Love it or hate it Andrei Verdes Maxcode
23 April 2016, Iasi -
Enhanced classes package ro.androidiasi.codecamp.main; @E<something> public class EnhancedClass{ … }
AA will generate… package ro.androidiasi.codecamp.main; public final class EnhancedClass_ extends EnhancedClass{ … } -
Enhanced classes package ro.androidiasi.codecamp.main; @E<something> public class EnhancedClass{ … }
AA will generate… package ro.androidiasi.codecamp.main; public final class EnhancedClass_ extends EnhancedClass{ … } -
BoringActivity public class BoringActivityextends Activity { @Override protected void onCreate(Bundle
savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); return true; } } -
BoringActivity - layout public class BoringActivityextends Activity { @Override protected
void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); return true; } } -
BoringActivity - menu public class BoringActivityextends Activity { @Override protected
void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); return true; } } -
BoringActivity - views private TabLayout mTabLayout; private ViewPager mViewPager; @Override
protected void onCreate(…) { super.onCreate(savedInstanceState); mTabLayout = (TabLayout) findViewById(R.id….); mViewPager = (ViewPager) findViewById(R.id…); } -
BoringActivity - views private TabLayout mTabLayout; private ViewPager mViewPager; @Override
protected void onCreate(…) { super.onCreate(savedInstanceState); this.setContentView(R.layout…); mTabLayout = (TabLayout) findViewById(R.id…); mViewPager = (ViewPager) findViewById(R.id…); } -
BoringActivity - fields private CodecampApp mCodecampApp; private SessionsAdapter mSessionsAdapter; private
LayoutInflater mLayoutInflater; mCodecampApp = (CodecampApp) this.getApplication(); mSessionsAdapter= new SessionsAdapter(); mLayoutInflater= (LayoutInflater) this.getSystemService( Context.LAYOUT_INFLATER_SERVICE); -
BoringActivity - fields private CodecampApp mCodecampApp; private SessionsAdapter mSessionsAdapter; private
LayoutInflater mLayoutInflater; mCodecampApp = (CodecampApp) this.getApplication(); mSessionsAdapter= new SessionsAdapter(); mLayoutInflater= (LayoutInflater) this.getSystemService( Context.LAYOUT_INFLATER_SERVICE); -
BoringActivity – menu item private MenuItem mMenuSearch; @Override … onCreateOptionsMenu(Menu
menu) { getMenuInflater().inflate(R.menu…., menu); mMenuSearch = menu.findItem(R.id.menu_search); return true; } -
BoringActivity – menu item private MenuItem mMenuSearch; @Override … onCreateOptionsMenu(Menu
menu) { getMenuInflater().inflate(R.menu…, menu); mMenuSearch = menu.findItem(R.id…); return true; } -
EnhancedActivity - application @App CodecampApp mCodecampApp; @Bean SessionsAdapter mSessionsAdapter; @SystemService
LayoutInflater mLayoutInflater; @OptionsMenuItem(R.id.menu_search) MenuItem mMenuSearch; -
EnhancedActivity - beans @App CodecampApp mCodecampApp; @Bean SessionsAdapter mSessionsAdapter; @SystemService
LayoutInflater mLayoutInflater; @OptionsMenuItem(R.id.menu_search) MenuItem mMenuSearch; -
EnhancedActivity - services @App CodecampApp mCodecampApp; @Bean SessionsAdapter mSessionsAdapter; @SystemService
LayoutInflater mLayoutInflater; @OptionsMenuItem(R.id.menu_search) MenuItem mMenuSearch; -
EnhancedActivity - menu item @App CodecampApp mCodecampApp; @Bean SessionsAdapter mSessionsAdapter;
@SystemService LayoutInflater mLayoutInflater; @OptionsMenuItem(R.id.menu_search) MenuItem mMenuSearch; -
EnhancedActivity - fields @App CodecampApp mCodecampApp; @Bean SessionsAdapter mSessionsAdapter; @SystemService
LayoutInflater mLayoutInflater; @OptionsMenuItem(R.id.menu_search) MenuItem mMenuSearch; • And that’s it! :D -
BoringActivity - extras public static final String TAG_EXTRA = "tag";
private String mSomeString; mSomeString = getIntent().getStringExtra(TAG_EXTRA); public static void start(Context pContext, String pSomeString){ Intent intent = new Intent(pContext, BoringActivity.class); intent.putExtra(TAG_EXTRA, pSomeString); pContext.startActivity(intent); } -
BoringActivity - extras public static final String TAG_EXTRA = "tag";
private String mSomeString; mSomeString = getIntent().getStringExtra(TAG_EXTRA); public static void start(Context pContext, String pSomeString){ Intent intent = new Intent(pContext, BoringActivity.class); intent.putExtra(TAG_EXTRA, pSomeString); pContext.startActivity(intent); } -
BoringActivity - extras public static final String TAG_EXTRA = "tag";
private String mSomeImportantString; mSomeString = getIntent().getStringExtra(TAG_EXTRA); public static void start(Context pContext, String pSomeString){ Intent intent = new Intent(pContext, BoringActivity.class); intent.putExtra(TAG_EXTRA, pSomeString); pContext.startActivity(intent); } -
BoringActivity - extras public static final String TAG_EXTRA = "tag";
private String mSomeImportantString; mSomeString = getIntent().getStringExtra(TAG_EXTRA); public static void start(Context pContext, String pSomeString){ Intent intent = new Intent(pContext, BoringActivity.class); intent.putExtra(TAG_EXTRA, pSomeString); pContext.startActivity(intent); } -
EnhancedActivity - extras @Extra String mSomeString; public static void start(Context
pContext, String pSomeString){ EnhancedActivity_ .intent(pContext) .mSomeString(pSomeString) .start(); } -
EnhancedActivity - summary @OptionsMenu(R.menu.main_menu) @EActivity(R.layout.activity_main) public class EnhancedActivityextends Activity {
@ViewById(R.id…) TabLayout mTabLayout; @ViewById(R.id…) ListView mListView; @App CodecampApp mCodecampApp; @Bean SessionsAdapter mSessionsAdapter; @SystemService LayoutInflater mLayoutInflater; @Extra String mSomeImportantString ; @OptionsMenuItem(R.id…)MenuItemmMenuSearch; } -
Class annotations • @EBean • @EService or @EIntentService • @EView
or @EViewGroup • @EActivity or @EFragment -
Method annotations - lifecycle 1. @AfterExtras public void afterExtrasInject(){...} 2.
@AfterInject public void afterMembersInject(){...} 3. @AfterViews public void afterViewsInjecta(){...} -
Method annotations - lifecycle 1. @AfterExtras public void afterExtrasInject(){...} 2.
@AfterInject public void afterMembersInject(){...} 3. @AfterViews public void afterViewsInjecta(){...} -
Method annotations - lifecycle 1. @AfterExtras public void afterExtrasInject(){...} 2.
@AfterInject public void afterMembersInject(){...} 3. @AfterViews public void afterViewsInjecta(){...} -
Method annotations – events @Click(R.id.button) public void onButtonClicked() { Toast.makeText(...).show();
} @LongClick(R.id.button) public void onButtonLongClicked(){ Toast.makeText(...).show(); } @ItemClick(R.id.list_view) public void onSessionsListItemClicked(Session pSession){ Toast.makeText(...).show(); } @TextChange(R.id.edit_text) public void onTextChangeEditText(...) { Toast.makeText(...).show(); } @OptionsItem(R.id.menu_search) public void onSearchMenuItemClicked() { Toast.makeText(...).show(); } -
Method annotations – events @Click(R.id.button) public void onButtonClicked() { Toast.makeText(...).show();
} @LongClick(R.id.button) public void onButtonLongClicked(){ Toast.makeText(...).show(); } @ItemClick(R.id.list_view) public void onSessionsListItemClicked(Session pSession){ Toast.makeText(...).show(); } @TextChange(R.id.edit_text) public void onTextChangeEditText(...) { Toast.makeText(...).show(); } @OptionsItem(R.id.menu_search) public void onSearchMenuItemClicked() { Toast.makeText(...).show(); } -
Method annotations – events @Click(R.id.button) public void onButtonClicked() { Toast.makeText(...).show();
} @LongClick(R.id.button) public void onButtonLongClicked(){ Toast.makeText(...).show(); } @ItemClick(R.id.list_view) public void onSessionsListItemClicked(Session pSession){ Toast.makeText(...).show(); } @TextChange(R.id.edit_text) public void onTextChangeEditText(...) { Toast.makeText(...).show(); } @OptionsItem(R.id.menu_search) public void onSearchMenuItemClicked() { Toast.makeText(...).show(); } -
Method annotations – events @Click(R.id.button) public void onButtonClicked() { Toast.makeText(...).show();
} @LongClick(R.id.button) public void onButtonLongClicked(){ Toast.makeText(...).show(); } @ItemClick(R.id.list_view) public void onSessionsListItemClicked(Session pSession){ Toast.makeText(...).show(); } @TextChange(R.id.edit_text) public void onTextChangeEditText(...) { Toast.makeText(...).show(); } @OptionsItem(R.id.menu_search) public void onSearchMenuItemClicked() { Toast.makeText(...).show(); } -
Method annotations – events @Click(R.id.button) public void onButtonClicked() { Toast.makeText(...).show();
} @LongClick(R.id.button) public void onButtonLongClicked(){ Toast.makeText(...).show(); } @ItemClick(R.id.list_view) public void onSessionsListItemClicked(Session pSession){ Toast.makeText(...).show(); } @TextChange(R.id.edit_text) public void onTextChangeEditText(...) { Toast.makeText(...).show(); } @OptionsItem(R.id.menu_search) public void onSearchMenuItemClicked() { Toast.makeText(...).show(); } -
Method annotations - threading @Background public void onBackgroundGetSessionsList(){ List<Session> sessionsList
= this.mCodecampRestClient.getSessions(); this.onUiThreadUpdateSessionsList(sessionsList); } @Background public void onBackgroundGetSessionById(String pSessionId){ Session session = this.mCodecampRestClient.getSession(pSessionId); this.onUiThreadUpdateSession(session); } @UiThread public void onUiThreadUpdateSessionsList(List<Session> pSessionList){ this.mSessionsAdapter.setSessionsList(pSessionList); this.mSessionsAdapter.notifyDataSetChanged(); } @UiThread public void onUiThreadUpdateSession(Session pSession){ this.mSessionsAdapter.setSession(pSession); this.mSessionsAdapter.notifyDataSetChanged(); } -
Method annotations - threading @Background public void onBackgroundGetSessionsList(){ List<Session> sessionsList
= this.mCodecampRestClient.getSessions(); this.onUiThreadUpdateSessionsList(sessionsList); } @Background public void onBackgroundGetSessionById(String pSessionId){ Session session = this.mCodecampRestClient.getSession(pSessionId); this.onUiThreadUpdateSession(session); } @UiThread public void onUiThreadUpdateSessionsList(List<Session> pSessionList){ this.mSessionsAdapter.setSessionsList(pSessionList); this.mSessionsAdapter.notifyDataSetChanged(); } @UiThread public void onUiThreadUpdateSession(Session pSession){ this.mSessionsAdapter.setSession(pSession); this.mSessionsAdapter.notifyDataSetChanged(); } -
REST Client - SpringAndroid @Rest(rootUrl = "https://awesomecodecampbackend.com"…) public interface CodecampRestClient{
@Get("/sessions") List<Session> getSessions(); @Get("/sessions/{sessionId}") Session getSession(@Path String sessionId); } -
REST Client - SpringAndroid @Rest(rootUrl = "https://awesomecodecampbackend.com”…) public interface CodecampRestClient{
@Get("/sessions") List<Session> getSessions(); @Get("/sessions/{sessionId}") Session getSession(@Path String sessionId); } -
Beware of the AA lifecycle multiple calls @EActivity public abstract
class ActivityA extends Activity{ @AfterViews public void afterViews(){} } @EActivity public static class ActivityB extends ActivityA { @AfterViews public void afterViews(){} } @EActivity public static class ActivityC extends ActivityB { @AfterViews public void afterViews(){} } -
Beware of the AA lifecycle multiple calls @EActivity public abstract
class ActivityA extends Activity{ @AfterViews public void afterViews(){} } @EActivity public abstract static class ActivityB extends ActivityA { @AfterViews public void afterViews(){} } @EActivity public static class ActivityC extends ActivityB { @AfterViews public void afterViews(){} } -
Beware of the AA lifecycle multiple calls @EActivity public abstract
class ActivityA extends Activity{ @AfterViews public void afterViews(){} } @EActivity public abstract class ActivityB extends ActivityA { @AfterViews public void afterViews(){} } @EActivity public class ActivityC extends ActivityB { @AfterViews public void afterViews(){} } -
Beware of the AA lifecycle multiple calls public final class
ActivityC_ extends ActivityC...{ @Override public void onViewChanged (HasViews hasViews){ afterViews(); afterViews(); afterViews(); } } -
Beware of the AA lifecycle multiple calls public final class
ActivityC_ extends ActivityC...{ @Override public void onViewChanged (HasViews hasViews){ afterViews(); afterViews(); afterViews(); } } -
Solution Use @AfterViews only once @EActivity public abstract class ActivityA
extends Activity{ @AfterViews public void afterViewsInject(){ this.afterViews(); } @CallSuper public void afterViews(){} } -
Solution Use @AfterViews only once @EActivity public abstract class ActivityA
extends Activity{ @AfterViews public void afterViewsInject(){ this.afterViews(); } @CallSuper public void afterViews(){} } -
Beware of the layout injection @EViewGroup(R.layout.view_awesome) public class ViewA extends
LinearLayout { } @EViewGroup public class ViewB extends ViewA { } -
Beware of the layout injection @EViewGroup(R.layout.view_awesome) public class ViewA extends
LinearLayout { } @EViewGroup public class ViewB extends ViewA { } -
Solution Always add your layout @EViewGroup(R.layout.view_awesome) public class ViewA extends
LinearLayout { } @EViewGroup(R.layout.view_awesome) public class ViewB extends ViewA { } -
Method naming @Background void onBackground*...(){} @UiThread void onUiThread*...(){} @UiThread(delay=...) void
onUiThreadDelayed*...(){} @Click void onSomethingClicked(){} @ItemClick void onSomethingItemClicked(){} @OptionsItem void onOptionsItemClicked(){} -
Method naming @Background void onBackground*...(){} @UiThread void onUiThread*...(){} @UiThread(delay=...) void
onUiThreadDelayed*...(){} @Click void onSomethingClicked(){} @ItemClick void onSomethingItemClicked(){} @OptionsItem void onOptionsItemClicked(){} -
Method naming @Background void onBackground*...(){} @UiThread void onUiThread*...(){} @UiThread(delay=...) void
onUiThreadDelayed*...(){} @Click void onSomethingClicked(){} @ItemClick void onSomethingItemClicked(){} @OptionsItem void onOptionsItemClicked(){} -
Method naming @Background void onBackground*...(){} @UiThread void onUiThread*...(){} @UiThread(delay=...) void
onUiThreadDelayed*...(){} @Click void onSomethingClicked(){} @ItemClick void onSomethingItemClicked(){} @OptionsItem void onOptionsItemClicked(){} -
Method naming @Background void onBackground*...(){} @UiThread void onUiThread*...(){} @UiThread(delay=...) void
onUiThreadDelayed*...(){} @Click void onSomethingClicked(){} @ItemClick void onSomeListItemClicked(){} @OptionsItem void onOptionsItemClicked(){} -
Method naming @Background void onBackground*...(){} @UiThread void onUiThread*...(){} @UiThread(delay=...) void
onUiThreadDelayed*...(){} @Click void onSomethingClicked(){} @ItemClick void onSomeListItemClicked(){} @OptionsItem void onSomeOptionsItemClicked(){} -
Beware of memory leaks @UiThread(delay = 5000) public void onUiThreadDelayedSomething(){
this.mActivity.finish(); } -
Solution Use WeakReferences @UiThread(delay = 5000) public void onUiThreadDelayedSomething (WeakReference<Activity>
pActivityWeakReference){ if(pActivityWeakReference.get() != null){ pActivityWeakReference.get().finish(); } } -
Beware of big projects • Classic Java errors can stop
AA from generating your enhanced classes • The error window won’t report more than 101 errors • Your actual error will get lost in the compiler stacktrace -
Solution 1.1 • Start a question on StackOverflow • Write
a regex that removes all of your “_;” ending imports • Fix the actual error and imports (no vcs? try local history from IntelliJ) • Make project • Voila! • Start a bounty on your StackOverflow error -
Solution 1.1 • Start a question on StackOverflow • Write
a regex that removes all of your “_;” ending imports • Fix the actual error and imports (no vcs? try local history from IntelliJ) • Make project • Voila! • Start a bounty on your StackOverflow error -
Solution 1.1 • Start a question on StackOverflow • Write
a regex that removes all of your “_;” ending imports • Fix the actual error and imports (no vcs? try local history from IntelliJ) • Make project • Voila! • Start a bounty on your StackOverflow error -
Solution 1.1 • Start a question on StackOverflow • Write
a regex that removes all of your “_;” ending imports • Fix the actual error and imports (no vcs? try local history from IntelliJ) • Make project • Voila! • Start a bounty on your StackOverflow error -
Solution 1.1 • Start a question on StackOverflow • Write
a regex that removes all of your “_;” ending imports • Fix the actual error and imports (no vcs? try local history from IntelliJ) • Make project • Voila! • Start a bounty on your StackOverflow error -
Solution 1.1 • Start a question on StackOverflow • Write
a regex that removes all of your “_;” ending imports • Fix the actual error and imports (no vcs? try local history from IntelliJ) • Make project • Voila! • Start a bounty on your StackOverflow error -
But wait, there’s more… • Someone answers the question... •
Change the javac compiler’s maxerr parameter to higher value (-Xmaxerrs 400 -Xmaxwarns 1000) -
But wait, there’s more… • Someone answers the question... •
Change the javac compiler’s maxerr parameter to higher value (-Xmaxerrs 400 -Xmaxwarns 1000) -
But wait, there’s more… • Someone answers the question... •
Change the javac compiler’s maxerr parameter to higher value (-Xmaxerrs 400 -Xmaxwarns 1000) • https://stackoverflow.com/questions/35314856/an droid-studio-messages- window/36628587#36628587 -
AA 4.0 • Major refactoring • Support for Parcelables with
parceler.org • Plugins • @AwesomeAnotation • AwesomePlugin extends AndroidAnnotationsPlugin • Let the fun begin! -
To use it or not to use it? That is
the question! Q&A? -
AndroidAnnotationsFramework – Love it or hate it Andrei Verdes Maxcode
23 April 2016, Iasi Please fill the online evaluation form after event