向 android 中的后退键添加功能以转到文件管理器中的上一个文件夹位置

adding functionality to back key in android to go to previous folder location in a file manager

我知道这个问题已经讨论了很多次,但我真的想不通。很抱歉又问了一遍。我正在制作 android 文件管理器。我在列表视图中显示文件和文件夹。我想向 "back" 键添加功能。当前随时按返回键都会退出应用程序。我希望它转到上一个文件夹(如果有)或以其他方式退出应用程序。我正在尝试 onBackPressed() 方法,但无法弄清楚应该在那里写什么。请帮我。

这是我的 MainActivity.java 文件:

 public class MainActivity extends ListActivity {
    private ArrayList<String> item = null;
    private ArrayList<String> path = null;
    private String root="/";
    private TextView myPath;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  /**super keyword in java is a reference variable that
         is used to refer immediate parent class object. */
        setContentView(R.layout.activity_main);
        myPath = (TextView)findViewById(R.id.path); /* storing the current path*/
        getDir(root);
    }

    private void getDir(String dirPath)
    {
        myPath.setText("Location: " + dirPath);
        item = new ArrayList<String>();
        path = new ArrayList<String>();
        File f = new File(dirPath);
        File[] files = f.listFiles(); /** files is an array of all the files in a directory. */
        if(!dirPath.equals(root))
        {
            item.add(root);
            path.add(root);
            item.add("../");
            path.add(f.getParent());
        }

        for(int i=0; i < files.length; i++)
        {
            File file = files[i];
            path.add(file.getPath());
            if(file.isDirectory())
                item.add(file.getName() + "/");
            else
                item.add(file.getName());
        }
        ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, item);
        setListAdapter(fileList);
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        File file = new File(path.get(position));
        if (file.isDirectory())
        {
            if(file.canRead())
                getDir(path.get(position));
            else
            {
                new AlertDialog.Builder(this)
                        .setIcon(R.drawable.icon)
                        .setTitle("[" + file.getName() + "] folder can't be read!")
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // TODO Auto-generated method stub
                                    }
                                }).show();
            }
        }

        else
        {
            new AlertDialog.Builder(this)
                    .setIcon(R.drawable.icon)
                    .setTitle("[" + file.getName() + "]")
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {

                                @Override

                                public void onClick(DialogInterface dialog, int which) {
                                    // TODO Auto-generated method stub
                                }
                            }).show();
    }
}

你可以试试这个:

@Override
protected void onBackPressed(){
    //super.onBackPressed(); //remove it if you want control
    String previousDir = "build your previous dir here";
    if (previousDir != null){ //if deferent root path
       Intent activityDir = new Intent(this, MainActivity.class);
       activityDir.putExtra("DIR_PATH", previousDir);
       startActivity(activityDir);
    }//end if

   finish(); //close this screen to show new screen above with new path
}

注意:您必须按以下代码编辑 onCreate(....):

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  /**super keyword in java is a reference variable that
         is used to refer immediate parent class object. */
        setContentView(R.layout.activity_main);
        myPath = (TextView)findViewById(R.id.path); /* storing the current path*/

       if(getIntent() != null){
           root = getIntent().getStringExtra("DIR_PATH", root); //if is null then get root
        }
        getDir(root);
    }