Mean-Street Mean-Street - 1 month ago 10
Android Question

Efficient implementation of File Explorer - Android

I'm creating a simple file explorer on Android Studio, and I wonder which one of the following implementations is the best:

My solution:
I have an activity called MainActivity that displays my list of files, and each time the user clicks on a directory, it creates a new MainActivity (and the onCreate method gets the new list of files, ...).

Correction:
In a correction from a tutorial, I found that when the user clicks on a directory, instead of starting a new activity, the code keeps the current one and changes everything (clear the list and fill it with new files, change the title...).

Is there a solution that is better than the other ? Is it more efficient to keep always the same activity ?

Thanks for any help.

Answer Source

Keep a single activity no question about it !

When a user clicks an item you build your data-source based on the newly selected path. Make sure to distinct between files and directories. Then simply call notifyDataSetChanged and thats it !

To query the file system there are two ways:

  1. The easy - use Java File.listFiles()
  2. The hard - run shell command Runtime.getRuntime().exec( "ls -la" ) and parse response.

There are many open source projects on github for the subject. Example:

Amaze File Manager