Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 395 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What do I use now that Handler() is deprecated?

#1
How do I fix the deprecation warning in this code? Alternatively, are there any other options for doing this?

Handler().postDelayed({
context?.let {
//code
}
}, 3000)
Reply

#2
Consider using coroutines

scope.launch {
delay(3000L)
// do stuff
}
Reply

#3
The deprecated function is that constructor for Handler. Use `Handler(Looper.myLooper()) .postDelayed(runnable, delay)` instead
Reply

#4
use this

```kotlin
Looper.myLooper()?.let {
Handler(it).postDelayed({
//Your Code
},2500)
}
```
Reply

#5
The handler() etc code is generated by the Android Studio 4.0.1 when a Fullscreen Activity, for example, is created from scratch. I know that we are being encouraged to use Kotlin, which I do, but from time to time I use sample projects to get an idea going.
It seems strange that we are chastised by AS when AS actually generates the code. It might be a useful academic activity to go through the errors and fix them but maybe AS could generate new clean code for us enthusiasts...
Reply

#6
Use Executor instead of handler for more info [Executor][1].<br/>
To achieve post delay use `ScheduledExecutorService`:

ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
Runnable runnable = () -> {
public void run() {
// Do something
}
};
worker.schedule(runnable, 2000, TimeUnit.MILLISECONDS);

[1]:

[To see links please register here]

Reply

#7
Provide a looper in the Handler Constructor

Handler(Looper.getMainLooper())
Reply

#8
If you want to avoid the null check thing in Kotlin (`?` or `!!`) you can use `Looper.getMainLooper()` if your `Handler` is working with some UI related thing, like this:

```
Handler(Looper.getMainLooper()).postDelayed({
Toast.makeText(this@MainActivity, "LOOPER", Toast.LENGTH_SHORT).show()
}, 3000)
```

Note: use `requireContext()` instead of `this@MainActivity` if you are using fragment.
Reply

#9
I usually use this one

Code:

Handler(Looper.myLooper() ?: return).postDelayed({
// Code what do you want
}, 3000)

Screenshot:

[![enter image description here][1]][1]


[1]:
Reply

#10
If you are using Variable for Handler and Runnable then use it like this.

private Handler handler;
private Runnable runnable;

handler = new Handler(Looper.getMainLooper());
handler.postDelayed(runnable = () -> {
// Do delayed stuff here
handler.postDelayed(runnable, 1000);
}, delay);
Also You need to remove callbacks in onDestroy()

@Override
public void onDestroy() {
super.onDestroy();
if (handler != null) {
handler.removeCallbacks(runnable);
}
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through