[英]how to go back to previous state of an activity?
我創建了一個名為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();
}
}
}
您需要覆蓋后退按鈕的行為:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//your code here
return true;
}
return super.onKeyDown(keyCode, event);
}
覆蓋super.onBackPressed()
@Override
public boolean onBackPressed(){
//your code
}
應該在2.3.3上工作
只需在點擊時調用finish()
即可。
//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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.