如何回到之前的活动状态?

时间:2012-04-03 14:15:45

标签: java android android-activity smb jcifs

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

 //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();
    }
}

}

4 个答案:

答案 0 :(得分:1)

您需要覆盖后退按钮的行为:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //your code here
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

答案 1 :(得分:1)

覆盖super.onBackPressed()

 @Override
 public boolean onBackPressed(){

 //your code
 }

应该在2.3.3上工作

答案 2 :(得分:0)

只需点击即可拨打finish()

答案 3 :(得分:0)

//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 = (ScrollView) findViewById(R.id.scrollview);
    LinearLayout layout= (LinearLayout) findViewById(R.id.linearlayout);
    //layout.setOrientation(0x00000001); set this in your xml file
    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++;
            }
        }
    }
    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();
    }
}

}

这就是你的布局xml文件(名为view.xml)看起来像这样(/res/layout/view.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<ScrollView android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/scrollview">
<LinearLayout 
    android:id="@+id/linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"> </LinearLayout>


</ScrollView>

</LinearLayout>