Handle 2 conditions in if - android

If(bool1&&bool2)
{
//some code
} else{
//some code
}
And here what I want is that even if 1 condition return true then also to proceed to run the code but I also want to know which condition returned true
Edit
I'm using RootTools library for checking root permission
RootTools.isRootAvailable() &&RootTools.isAcessgiven()
So if
RootTools.isAcessgiven() returns false
I can tell root is not proper but if
RootTools. isAcessgiven() returns true and RootTools. isRootAvailable () returns true
I can tell him root is proper

If you just want to know which one returned true, you could just log it:
if (bool1 || bool2) {
log.i("bool1: " + bool1 ", bool2: " + bool2)
code......
}

then you need two if statements.
if(bool1) {
// code
} else if(bool2) {
// slightly different code
} else {
// final code
}
no way around it

You mean proceed to run code "//someother code" ?
if(bool1 || bool2){
// fun first code for any of bool1 or bool2 - true
// you need to check which one was true - bool1, or bool2 in other statement.
} else {
// run this if bool1 and bool2 is false
}

The one possible answer could be , Here both the if block will run .
if(bool1){
// some code
// print(in bool 1)
}
if(bool2)
{
// some code
// print(in bool 2)
}
The other answer you can do is my maintaining a another variable
boolean val1,val2;
if(val1=bool1 || val2=bool2 )
{
}

Related

Android Studio - Condition is always true?

This would be the first time I post a question since I couldn't find the answer to this. Something really weird is happening with my if/else statements. My code was working perfectly for the past week, but recently it kept on telling me that a statement is always true?
My code is the following:
int checking = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checking != 1) {
speaker.setChecked(true);
} else if (checking == 1) {
speaker.setChecked(false);
}
}
This says checking == 1 is always true which makes sense, but if I switch it around to:
if (checking == 1) {
speaker.setChecked(true);
} else if (checking != 1) {
speaker.setChecked(false);
}
This says that checking != 1 is always true as well. Can someone help?
It's just a logic of editor. In first if statement, you check checking == 1 and the second if statement, you negative the statement. The editor will understand the second if statement alway true
I think you should replace else if to else. Sorry my bad English.
The reason why you are seeing this warning is:
You have set int checking = 1 and you are either checking:
if (checking != 1) which is always true
Or checking == 1 which is also always true
Due to this other else if condition will not be executed at all.
The warning will be only removed if you try to change value of checking either at runtime or using some conditions at compile time.
If your intention is just to set the speaker.setChecked() you can do following:
speaker.setChecked(checking == 1)
Note: This will not remove the warning
Try this
int checking = 1;
boolean isChecked = false;
isChecked = ((checking == 1) ? true : false)
speaker.setChecked(isChecked)

How to Listen to more than Three Fields in Observables.combineLatest

I have code to listen to exactly three fields using Observables.combineLatest
Observables.combineLatest(text_name.asObservable(),text_username.asObservable(), text_email.asObservable()).subscribe({ t ->
if (t.first.toString() != currentName || t.second.toString() != currentUsername) {
startActionMode()
} else {
finishActionMode()
}
})
but when I add another parameter to the Observables.combineLatest it throughs error since only 3 inline-parameters can be passed..
Now I would wish to pass 4 parameters in the parameter list for Observables.combineLatest.. I know it should be done using an array or a list, passed in as parameter but It's hard for me to figure it out using Kotlin.
Help me out.. Thanks in Advance..
You need to add a combine function if you want to combine more than 3 observables. You can do something like this.
Observables.combineLatest(
first.asObservable(),
second.asObservable(),
third.asObservable(),
forth.asObservable()
)
// combine function
{ first, second, third, forth->
// verify data and return a boolean
return#subscribe first.toString() != currentName || second.toString() != currentUsername
}
.subscribe({ isValid->
if (isValid) {
startActionMode()
} else {
finishActionMode()
}
})
In the combine function you can verify your data and return a boolean.
Then in subscribe you can take an action based on that boolean

Cannot understand: 'calledOtherActivity'

I've recently found a code that could solve my background service problem, but I don't understand one part of it. Could you tell me what should I write in !calledOtherActivity? This part is red in my code and the hint says: " cannot resolve symbol 'calledOtherActivity' "
Code
#Override
public void onPause() {
if(!isFinishing()){
if(!calledOtherActivity){
stopService(serviceRef);
}
}
}
let's understand the Situation
if( condition )
{
// if condition is true it goes here
}
else
{
// bah condition is false meaning !true
}
If the condition in the above statement is false, then the statements in the else block will always be executed.
If it is true it goes inside it like i commented
This condition only can be true or false
and since you have only one in the condition called calledOtherActivity it needs to carry true or false so its a boolean
private boolean calledOtherActivity;
by default its value is false
looking to your condition it is if(!calledOtherActivity) as i said to go inside this it needs to be true
! <--- this is NOT operator this inverts the value of a boolean
so if you pass calledOtherActivity with a false value because of this NOT operator the full output of the condition becomes true

Android executing both IF and ELSE statement together

I am working with FireBase Notifications and I can send a notification which will send the user to the webview page I input on the console.
The problem is that when it matches the IF statement is fires the else statement too, what could be the cause of this?
if(getIntent().getExtras()!=null) {
for (String key : getIntent().getExtras().keySet()) {
if (key.equals("url")){
mwebView.loadUrl("http://example.com/" + getIntent().getExtras().getString(key));
}else {
mwebView.loadUrl("http://example.com");
}
}
}
Because it executes both at the same time the app crashes.
Also when I load the app the usual way it matches the with:
if(getIntent().getExtras()!=null)
and then loads the else statement. Shouldnt getExtras be null?
When I first install a new instance of the app it uses the following statement:
if(getIntent().getExtras()==null) {
if (haveNetworkConnection()) {
mwebView.loadUrl("http://example.com");
} else {
mwebView.loadUrl("file:///android_asset/myerrorpage.html");
}
}
Update:
As I cannot find out why this it happening I am trying another approach, How would I get the variable outside of the loop to use like the following:
if(getIntent().getExtras()!=null) {
for (String key : getIntent().getExtras().keySet()) {
String valuex = getIntent().getExtras().getString(key);
}
}
if (haveNetworkConnection()) {
mwebView.loadUrl("http://example.com/" + valuex);
} else {
mwebView.loadUrl("file:///android_asset/myerrorpage.html");
}
If and Else can not both be executed in one go.
You should check your other code and ensure, that this code section is not executed twice for some reason (once with TRUE and once with FALSE).

"If-else" statement going down the wrong section in step debug

Hopefully a simple answer but I'm a little baffled. I'm expecting the code to go down the first if section below, but it always goes to the else.
When I get to line on a breakpoint >> if (url2!=null && !url2.isEmpty())
In the expressions window:
url2 IS "???/wp-content/uploads/2011/01/toonieJune10_091-640x334.jpg"
url2!=null IS true
!url2.isEmpty() IS true
However when debugging it always seems to hit the else, even though both conditions are true. I'm suspecting something is out of sync with my built code somehow as the step through debugging seems to give me inconsistencies.
I've tried cleaning the code and making some changes in the class and recompiling etc.
Help is much appreciated! Thanks!
public String getImageBannerUrl()
{
if (getPhotoFile1()!=null) return getPhotoFile1().getUrl();
String url2 = getRemoteImageUrl();
if (url2!=null && !url2.isEmpty())
{
return url2;
}
else
{
//Otherwise get default image based on category
return getImageCategoryUrl();
}
}
Try somthing like..
public String getImageBannerUrl()
{
if ((!getPhotoFile1().isEmpty()) && (!getPhotoFile1().matches(" "))) return getPhotoFile1().getUrl();
String url2 = getRemoteImageUrl();
if ((!url2.isEmpty()) && (!url2.matches(" ")))
{
return url2;
}
else
{
//Otherwise get default image based on category
return getImageCategoryUrl();
}
}
Note : here getPhotoFile1() must be returning String value..

Categories

Resources