Kelyn Ferguson Kelyn Ferguson - 1 month ago 8
Android Question

How do I stop file navigation from going above a certain directory?

I'm quite new to Android so this might be a quirk of the platform that I'm not yet accustomed to, but I'm having an issue with file navigation. I've successfully created a system where I can enter into and navigate out of directories successfully, but I am unable to set my app to disallow navigation above

/storage/emulated/0
. My current code for doing this is:

public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
if(f.getAbsolutePath() != "/storage/emulated/0"){
RefreshListing(f.getParent());
}
return true;
}
return super.onKeyDown(keyCode,event);
}


Where
f
is a variable of type
File
which contains the current directory.

I have tried adding a slash to the end of my compared path, which doesn't make a difference. I have also tried printing
f.getAbsolutePath()
to the screen to make sure I am using the correct syntax, but still the strings do not equate and navigation ends up going past
/storage/emulated/0
, crashing the app.

Answer

In Java, string comparison is via equals(), not == or !=.

Comments