我创建了一个使用 TabActivity 的应用程序。一切都还好,直到某一刻……
每次切换选项卡时,来自 tabHost 的活动都会被销毁并重新创建。我记录了 Activity 的每个状态,发现当 tab 获得焦点时被称为 methosd:onCreatr()、onStart()、onResume(),而当 tab 失去焦点时被称为 onStop()、onDestroy()
如何在不破坏和重新创建活动的情况下切换选项卡。???
对不起我的英语不好 :)
主类
package my.pack;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class Main extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
TabHost.TabSpec spect;
Intent intent;
intent = new Intent().setClass(this, ListDevice.class);
spect = tabHost.newTabSpec("test").setIndicator("Device").setContent(intent);
tabHost.addTab(spect);
intent = new Intent().setClass(this, ListServer.class);
spect = tabHost.newTabSpec("test").setIndicator("Server").setContent(intent);
tabHost.addTab(spect);
tabHost.setCurrentTab(0);
}
}
ListServer.class(用于tabHost)
package my.pack;
import java.io.File;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
public class ListServer extends ListActivity {
private File file = new File(".");
private ArrayList<File> values = new ArrayList<File>();
private CopyOfMyPerformanceArrayAdapter adapter;
public void onCreate(Bundle icicle) {
Log.d("test","server");
super.onCreate(icicle);
for (File f: file.listFiles()) {
if (f.isDirectory()) values.add(f);
}
for (File f: file.listFiles()) {
if (f.isFile()) values.add(f);
}
View footer = getLayoutInflater().inflate(R.layout.footer, null);
getListView().addFooterView(footer);
adapter = new CopyOfMyPerformanceArrayAdapter(this,values);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
try{
File file = (File) getListAdapter().getItem(position);
if (file.isDirectory()) {
adapter.clear();
for (File f: file.listFiles()) {
values.add(f);
}
adapter.notifyDataSetChanged();
}} catch(Exception e) { Log.d("opa", "server");}
}
public void onRestart(){
Log.d("ftp","onRestart");
super.onRestart();
}
public void onStart(){
Log.d("ftp","onStart");
super.onStart();
}
public void onResume(){
Log.d("ftp","onResume");
super.onResume();
}
public void onPouse(){
Log.d("ftp","onpouse");
super.onPause();
}
public void onStop(){
Log.d("ftp","onStop");
super.onStop();
}
public void onDestroy(){
Log.d("ftp","onDestroy");
super.onDestroy();
}
}