How to implement timer with Kotlin coroutines

Edit: note that the API suggested in the original answer is now marked @ObsoleteCoroutineApi: Ticker channels are not currently integrated with structured concurrency and their api will change in the future. You can now use the Flow API to create your own ticker flow: import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds import kotlinx.coroutines.* import kotlinx.coroutines.flow.* fun tickerFlow(period: Duration, … Read more

Kotlin Android debounce

I’ve created a gist with three debounce operators inspired by this elegant solution from Patrick where I added two more similar cases: throttleFirst and throttleLatest. Both of these are very similar to their RxJava analogues (throttleFirst, throttleLatest). throttleLatest works similar to debounce but it operates on time intervals and returns the latest data for each … Read more

How to hide the soft keyboard inside a fragment?

As long as your Fragment creates a View, you can use the IBinder (window token) from that view after it has been attached. For example, you can override onActivityCreated in your Fragment: @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getView().getWindowToken(), 0); }

Why do we use “companion object” as a kind of replacement for Java static fields in Kotlin?

What is the intended meaning of “companion object”? Why is it called “companion”? First, Kotlin doesn’t use the Java concept of static members because Kotlin has its own concept of objects for describing properties and functions connected with singleton state, and Java static part of a class can be elegantly expressed in terms of singleton: … Read more

Stop fragment refresh in bottom nav using navhost

Try this: public class MainActivity extends AppCompatActivity { final Fragment fragment1 = new HomeFragment(); final Fragment fragment2 = new DashboardFragment(); final Fragment fragment3 = new NotificationsFragment(); final FragmentManager fm = getSupportFragmentManager(); Fragment active = fragment1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); … Read more

Android : Permissions check for BLE [closed]

Google made Android more strict by adding the new BLUETOOTH_CONNECT and BLUETOOTH_SCAN permissions. You will get SecurityException in runtime if you attempt to access any BLE API that requires these permissions. So we need to check the permissions in the activity which is set to android.intent.action.MAIN in the manifest file. I call that as MainActivity. … Read more