Android Lifecycle Aware Modal

Sometimes we want to show the user an alert when somethings wrong or we just need to give some extra info, For example; Ask the user if he is sure he wanna leave the app. This can be achieved with the following code (runs inside activity): AlertDialog.Builder(this) .setMessage("Are you sure you want to exit?") .setCancelable(false) .setPositiveButton("Yes", object:DialogInterface.OnClickListener() { fun onClick(dialog:DialogInterface, id:Int) { this@YourActivity.finish() } }) .setNegativeButton("No", null) .show() This works fine, buy there is one annoying issue, If user clicks home button while dialog displayed, and go back to the app, the dialog will still be there. Now your user experience is seeing a question about getting out of the app while he just went in. ...

October 25, 2018 · 2 min · Chen Kinnrot

Live Data Pitfall You Should Be Aware Of

When working with MutableLiveData you can update the observable value in 2 ways: setValue postValue Both will update the live data value as expected as long as your code is running from the main thread. If you need to update a value from other thread you can use the postValue which is thread safe, and will make sure to notify observers on main thread. This is all nice, but be aware! ...

October 22, 2018 · 2 min · Chen Kinnrot