Kelyn Ferguson Kelyn Ferguson - 11 days ago 6x
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

. 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"){
return true;
return super.onKeyDown(keyCode,event);

is a variable of type
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
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
, crashing the app.


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