listView和隐式intent的意外行为

时间:2017-09-16 20:08:22

标签: java android

我正在开发基于listView的文件浏览器。 概念很简单:listView填充了外部和内部存储中的用户文件。用户可以通过单击listView的项目来查看层次结构,或者通过按后退按钮返回到父级

如果该项目是文件(例如file.mp3),那么会向系统发送意图,系统会显示一个能够处理该文件的其他应用程序面板

一切正常但是当其他应用程序的面板显示然后关闭时(通过按后退按钮或向下滑动),我需要按两次后退按钮返回到父文件..这就是问题所在:我不知道为什么两次,为什么不只是一次......

我已经弄明白为什么但是我没有得到它...... 我该怎么做才能解决问题?

这是发生问题的代码部分

public class Frag2 extends Fragment {

private View root;

private File path;
private ArrayList<File> subPaths;

private ListView mListView;

private MyFileExplorerAdapter adapter;

public static boolean pause = false;

public void returnToParent(File f) {

    File parent = f.getParentFile();

    if(parent.getPath().equals("/mnt"))
        subPaths = getStoragesPath();

    else {
        path = parent;
        subPaths = updateSubPaths(path);
    }
    refresh();

}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    root = getLayoutInflater().inflate(R.layout.frag2,null);
    mListView = root.findViewById(R.id.listView);

    subPaths = getStoragesPath();
    refresh();

    mListView.setOnItemClickListener((adapterView, view, i, l) -> {

        path = subPaths.get(i);

        if(path.isDirectory()) {
            subPaths = updateSubPaths(path);
            refresh();
        }
        else {
            launchActivity();
        }

    });

}

private void refresh() {

    adapter = new MyFileExplorerAdapter(getContext(), R.layout.frag2, subPaths);
    mListView.setAdapter(adapter);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return root;
}
@Override
public void onResume() {
    super.onResume();
    pause = false;
    if(path != null)
        refresh();

}

@Override
public void onPause() {
    super.onPause();
    pause = true;
}

private boolean isExternalStorageWritable() {

    // get the external storage state
    String state = Environment.getExternalStorageState();

    // return true if the external storage is mounted
    return state.equals(Environment.MEDIA_MOUNTED);
}

private ArrayList<File> getStoragesPath() {

    ArrayList<File> al = new ArrayList<>();

    if(isExternalStorageWritable())
        al.add(new File(Util.EXTERNAL_PATH));

    al.add(new File(Util.INTERNAL_PATH));

    return al;
}

private ArrayList<File> updateSubPaths(File currentPath) {

    ArrayList<File> al = new ArrayList<>();

    if(currentPath.listFiles() != null && OptionsActivity.isHiddenFilesVisible)
        al.addAll(Arrays.asList(currentPath.listFiles()));

    else if(currentPath.listFiles() != null && !OptionsActivity.isHiddenFilesVisible)
        al.addAll(Arrays.asList(currentPath.listFiles(new MyFileFilter())));

    return al;
}

private void launchActivity() {

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(path);

    String extension = Util.extension(path);

    if(Util.VIDEO.contains(extension))
        intent.setDataAndType(uri,"video/*");
    else if(Util.AUDIO.contains(extension))
        intent.setDataAndType(uri,"audio/*");
    else if(Util.IMAGE.contains(extension))
        intent.setDataAndType(uri,"image/*");
    else if(Util.TEXT.contains(extension))
        intent.setDataAndType(uri,"text/*");
    else if(Util.APPLICATION.contains(extension))
        intent.setDataAndType(uri,"application/*");

    getActivity().startActivity(intent);

}

public File getPath() {
    return path;
}

}

0 个答案:

没有答案