gpt4 book ai didi

java - 如何返回到 Activity 之前的状态?

转载 作者:行者123 更新时间:2023-12-02 07:53:06 25 4
gpt4 key购买 nike

我创建了一个名为 viewActivity 的 Activity 。它显示Windows系统中的共享文件和文件夹。我正在使用 jcifs.smb 包中的函数 listFiles() 。最初显示共享驱动器。我选择了递归调用 listFiles() 函数。也就是说,当我单击驱动器中的特定文件夹时,会显示该文件的内容。当我再次单击上一个文件夹内的文件夹时,也会显示其内容。这里的问题是,当我按下 Android 中的后退按钮时,它会调用上一个 Activity,而不是返回到上一个文件夹。那么如何实现这个功能呢?

 //my package
package com.android.accesspc;

import java.net.MalformedURLException;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

public class viewActivity extends Activity{

int id=0;
SmbFile previous=null;
int count=0;
void list(SmbFile obj)
{
ScrollView sv = new ScrollView(this);
LinearLayout layout=new LinearLayout(this);
layout.setOrientation(0x00000001);
try
{
SmbFile a[]=obj.listFiles();
for(final SmbFile m:a)
{
if(!m.isHidden())
{
Button btn = new Button(this);
btn.setId(id);
if(m.isDirectory())
{
btn.setText(m.getName().replace("/",""));
btn.setTextSize((float)20);
btn.setOnClickListener(new
View.OnClickListener()
{
public void onClick(View v)
{
previous=m;//used to hold previous smb object
count++;//takes count of no of folders pressed
list(m);
}
});
}
else
{
btn.setText(m.getName());
}
layout.addView(btn);
id++;
}
}
sv.addView(layout);
setContentView(sv);
}
catch(SmbException e)
{
Toast displayMsg=Toast.makeText(this,"cannot list files",Toast.LENGTH_SHORT);
displayMsg.show();
}
}

String ip,name,password;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.view);

ip="192.168.0.100";//my ip address
name="abc";//my windows username
password="abcd";//my windows password

SmbFile dir=null;
String url= "smb://" + ip + "/";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, name, password);
try
{
dir = new SmbFile(url, auth);
}
catch (MalformedURLException e1)
{
Toast displayMsg=Toast.makeText(this,"Network error",Toast.LENGTH_SHORT);
displayMsg.show();
}
//used to list files and folders of smb object initially
list(dir);
}

@Override
public void onBackPressed() {
// TODO Auto-generated method stub
if(!(count==0))
{
//if count is not 0,that means the user has clicked a folder
//we have "previous" holding previous smb value which has to be called now
list(previous);
}
else
{
//the default behaviour
super.onBackPressed();
}
}

}

最佳答案

重写 super.onBackPressed()

 @Override
public boolean onBackPressed(){

//your code
}

应该适用于 2.3.3

关于java - 如何返回到 Activity 之前的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9995137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com