Java 具有listView和隐式意图的意外行为

Java 具有listView和隐式意图的意外行为,java,android,Java,Android,我正在开发一个基于listView的文件资源管理器。 概念很简单:listView在外部和内部存储器中填充用户的文件。用户可以通过单击listView的项来遍历层次结构,或者通过按back按钮返回到父级 如果该项目是一个文件(例如file.mp3),则会向系统发送一个意图,系统会显示一个能够处理该文件的其他应用程序面板 一切正常,但当其他应用程序的面板显示然后关闭时(通过按后退按钮或向下滑动),我需要按后退按钮两次以返回到父文件。。这就是问题所在:我不知道为什么会有两次,为什么不会只有一次 我已

我正在开发一个基于listView的文件资源管理器。 概念很简单:listView在外部和内部存储器中填充用户的文件。用户可以通过单击listView的项来遍历层次结构,或者通过按back按钮返回到父级

如果该项目是一个文件(例如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;
}

}
公共类Frag2扩展片段{
私有视图根;
私有文件路径;
私有ArrayList子路径;
私有列表视图;
私有MyFileExplorerAdapter适配器;
公共静态布尔暂停=false;
公共无效returnToParent(文件f){
File parent=f.getParentFile();
if(parent.getPath()等于(“/mnt”))
子路径=getStoragesPath();
否则{
路径=父级;
子路径=更新子路径(路径);
}
刷新();
}
@凌驾
创建时的公共void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
root=getLayoutFlater().充气(R.layout.frag2,null);
mListView=root.findviewbyd(R.id.listView);
子路径=getStoragesPath();
刷新();
mListView.setOnItemClickListener((adapterView,view,i,l)->{
路径=子路径get(i);
if(path.isDirectory()){
子路径=更新子路径(路径);
刷新();
}
否则{
启动活动();
}
});
}
私有无效刷新(){
适配器=新的MyFileExplorer适配器(getContext(),R.layout.frag2,子路径);
mListView.setAdapter(适配器);
}
@可空
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
返回根;
}
@凌驾
恢复时公开作废(){
super.onResume();
暂停=错误;
if(路径!=null)
刷新();
}
@凌驾
公共无效暂停(){
super.onPause();
暂停=正确;
}
私有布尔值isExternalStorageWritable(){
//获取外部存储状态
String state=Environment.getExternalStorageState();
//如果已装入外部存储器,则返回true
返回状态.equals(环境.MEDIA_安装);
}
私有ArrayList getStoragesPath(){
ArrayList al=新的ArrayList();
if(isExternalStorageWritable())
al.add(新文件(Util.EXTERNAL_PATH));
al.add(新文件(Util.INTERNAL_PATH));
返回al;
}
私有ArrayList更新子路径(文件当前路径){
ArrayList al=新的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(newmyfilefilter()));
返回al;
}
私有void启动活动(){
意向意向=新意向(意向.行动\视图);
Uri=Uri.fromFile(路径);
字符串扩展名=Util.extension(路径);
if(Util.VIDEO.contains(扩展))
intent.setDataAndType(uri,“video/*”);
else if(Util.AUDIO.contains(扩展))
intent.setDataAndType(uri,“音频/*”);
else if(Util.IMAGE.contains(扩展))
intent.setDataAndType(uri,“image/*”);
else if(Util.TEXT.contains(扩展))
intent.setDataAndType(uri,“text/*”);
else if(Util.APPLICATION.contains(扩展))
setDataAndType(uri,“application/*”);
getActivity().startActivity(意图);
}
公共文件getPath(){
返回路径;
}
}

有人有想法吗?有人有想法吗?