Reputation: 65
I just created boolean method, which should at the end return some boolean variable... BUT I want to return it 1 second later (some operations is making in app) This code but don't work... What should I do?
private boolean variable;
public boolean Method(String device) {
//some code here
//then postdelayed
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//variable is set in other part of app
return variable;
}
}, 1000);
}
Upvotes: 1
Views: 2098
Reputation: 6707
You can move that handler to the part where you want to call and get that boolean variable, like:
private String device;
private boolean variable;
public void doSomething() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (getBoolean(device)) {
// Do something.
}
}
}, 1000);
}
public boolean getBoolean(String device) {
// Some code...
return variable;
}
Upvotes: 0
Reputation: 93678
You don't. You need to rearchitecture what you're doing. The only way to delay a return is to hold up the UI thread, which is not the right way to do anything ever. What you need to do is make any code that needs the return value execute in the postDelayed Runnable. What you want to do will never work.
Upvotes: 2