"A new way of thinking about Android"
by Pablo Arqueros
Architect (of buildings...)
Android dev engineer
Lover Design UX/UI
email: pearqueros@gmail.com
twitter: @pearqueros
Kotlin is a Concise, Safe and Statically typed programming language focused on Interoperability with Java.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)
val fab = findViewById(R.id.fab) as FloatingActionButton
fab.setOnClickListener { view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show() }
}
}
Java (19 lines)
Kotlin (12 lines)
Extend and Implement
The words “extends” and “implement” were replaced by a colon “:”
Fun - ctions
class MainActivity : AppCompatActivity()
We don’t have our classic “public void methodName()” structure.
But where is the type returned from our “onCreate” method?
override fun onCreate(savedInstanceState: Bundle?) {
override fun onCreate(savedInstanceState: Bundle?) : Unit {
Bye Semicolon;
In Kotlin you don’t have to put the semicolon at the end of a sentences but still you can do it if you want (don’t do it).
Values and Variable
val price = 100 // Int
price = 30 // don't compile! it's a constant
var total = price * 3 // Int
val name = "Juancho" // String
val lastname : String = "Keddit" // explicit type definition var size : Double = 30.0 var time : Float = 15f
Properties and Fields
resources.getString(R.string.id) // vs getResources().getString(R.string.id) // still allowed
Safe?:Safe!
val a : String = null // don't compile! var b : Int // neither as must be initialized or abstract. val ok : String? = null // OK :)
everything in Kotlin is not nullable unless you specifically declare it in this way
Safe call
val context : Context? = null val res = context?.getResources() // not crash, res will be null
Smart cast
val context : Context? = null if (context != null) { val res = context.getResources() // Don't need '?' anymore val appName = res.getString(R.string.app_name) val shortName = appName.substring(0, 2) } val context : Context? = null context?.let { val res = context.getResources() // Don't need '?' anymore val appName = res.getString(R.string.app_name) val shortName = appName.substring(0, 2) }
NewsFragment.kt
NewsAdapter
NewsManager
fun getNews(): Observable<List<RedditNewsItem>> { ... }
NewsManager
val subscription = newsManager.getNews().subscribe ( { retrievedNews -> ... }, { e -> ... }
NewsManager
val subscription = newsManager.getNews() .subscribeOn(Schedulers.io()) .subscribe (...)
NewsManager
val subscription = newsManager.getNews() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe ( ... )
AndroidSchedulers.mainThread()
...and finally....
Generated Code
What happened after this?
Familiarity with Dagger 2 Library
Base elements (annotations) of Dagger 2
Tree representation of the dependency graph for our example repo
Graph generated by daggraph
ApplicationModule
Activity Module
ApplicationModule
@Module
public class ApplicationModule {
private final Application mApplication;
public ApplicationModule(Application app) {
mApplication = app;
}
@Provides
@ApplicationContext
Context provideContext() {
return mApplication;
}
@Provides
Application provideApplication() {
return mApplication;
}
...
@Provides
@DatabaseInfo
String provideDatabaseName() {
return "demo-dagger.db";
}
@Provides
@DatabaseInfo
Integer provideDatabaseVersion() {
return 2;
}
@Provides
SharedPreferences provideSharedPrefs() {
return mApplication.getSharedPreferences("demo-prefs", Context.MODE_PRIVATE);
}
}
ActivityModule
@Module
public class ActivityModule {
private Activity mActivity;
public ActivityModule(Activity activity) {
mActivity = activity;
}
@Provides
@ActivityContext
Context provideContext() {
return mActivity;
}
@Provides
Activity provideActivity() {
return mActivity;
}
}
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(DemoApplication demoApplication);
@ApplicationContext
Context getContext();
Application getApplication();
DataManager getDataManager();
SharedPrefsHelper getPreferenceHelper();
DbHelper getDbHelper();
}
ApplicationComponent
ActivityComponent
@PerActivity
@Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class)
public interface ActivityComponent {
void inject(MainActivity mainActivity);
}
@provide annotations
ApplicationComponent
Thanks for your time!!!
Questions?