FirzenYogesh / FileListerDialog

A simple file/ directory picker dialog for android
Apache License 2.0
443 stars 78 forks source link

FileListerDialog disappears on screen rotation. #8

Open isabsent opened 6 years ago

isabsent commented 6 years ago

It would be better to extend FileListerDialog from DialogFragment to prevent it from disappearing on screen rotations. Can you fix it?

isabsent commented 6 years ago

The code of modified dialog FileListerDialogMod may looks as below:

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.Window;

import java.io.File;

public class FileListerDialogMod extends DialogFragment {
    private FilesListerView filesListerView;
    private OnFileSelectedListener onFileSelectedListener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        filesListerView = new FilesListerView(getActivity());
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
                .setTitle(getTitle(filesListerView))
                .setView(filesListerView)
//                .setIcon(R.drawable.ic_action_pick)
                .setPositiveButton("Select", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (onFileSelectedListener != null)
                            onFileSelectedListener.onFileSelected(filesListerView.getSelected(), filesListerView.getSelected().getAbsolutePath());
                    }
                })
                .setNeutralButton("Default Dir", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //filesListerView.goToDefaultDir();
                    }
                })
                .setNegativeButton("Cancel", null);
        filesListerView.start();
        final AlertDialog alertDialog = builder.create();
        alertDialog.setCanceledOnTouchOutside(false);
        return alertDialog;
    }

    private String getTitle(FilesListerView filesListerView){
        switch (filesListerView.getFileFilter()) {
            case DIRECTORY_ONLY:
                return "Select a directory";
            case VIDEO_ONLY:
                return"Select a Video file";
            case IMAGE_ONLY:
                return"Select an Image file";
            case AUDIO_ONLY:
                return"Select an Audio file";
            case ALL_FILES:
                return"Select a file";
            default:
                return"Select a file";
        }
    }

    /**
     * Listener to know which file/directory is selected
     *
     * @param onFileSelectedListener Instance of the Listener
     */
    public void setOnFileSelectedListener(OnFileSelectedListener onFileSelectedListener) {
        this.onFileSelectedListener = onFileSelectedListener;
    }

    /**
     * Set the initial directory to show the list of files in that directory
     *
     * @param file File pointing to the directory
     */
    public void setDefaultDir(@NonNull File file) {
        filesListerView.setDefaultDir(file);
    }

    /**
     * Set the initial directory to show the list of files in that directory
     *
     * @param file String denoting to the directory
     */
    public void setDefaultDir(@NonNull String file) {
        filesListerView.setDefaultDir(file);
    }

    /**
     * Set the file filter for listing the files
     *
     * @param fileFilter One of the FILE_FILTER values
     */
    public void setFileFilter(FILE_FILTER fileFilter) {
        filesListerView.setFileFilter(fileFilter);
    }

    public enum FILE_FILTER {
        /**
         * List All Files
         */
        ALL_FILES,
        /**
         * List only directories
         */
        DIRECTORY_ONLY,
        /**
         * List Directory and Image files
         */
        IMAGE_ONLY,
        /**
         * List Directory and Video files
         */
        VIDEO_ONLY,
        /**
         * List Directory and Audio files
         */
        AUDIO_ONLY
    }
}

and the usage:

FileListerDialogMod fileListerDialog = new FileListerDialogMod();
fileListerDialog.setOnFileSelectedListener(new OnFileSelectedListener() {

    @Override
    public void onFileSelected(File file, String path) {
        System.out.println(path);
    }
});
fileListerDialog.show(getFragmentManager(), FileListerDialogMod.class.getName());

If you use above code there is one more thing to do - show a dialog with the folder which was selected before the rotation.