我有一个应用程序,当它遍历文件系统时每个文件夹一个活动,就会递归创建一个活动的多个实例。我在活动堆栈中向前移动时通过Intent检索数据(即正常的文件夹遍历)。当我从父文件夹转到子文件夹时,应用程序运行良好。我有问题[返回],因为文件/文件夹列表与前一个活动(孩子)保持一致。我不想再从互联网上检索文件夹数据,因此可以跟踪以前的活动状态吗?这是我活动的一段代码。感谢
public class RisorseActivity extends DashboardActivity implements Observer {
private Docente docente;
private String get_response;
private String response;
//private HttpsClient client;
private ListView list;
private RelativeLayout progressLayout;
private ProgressBar progressBar;
private TextView txtMateriale;
private ArrayList<Risorsa> risorseList;
private RisorseAdapter risAdapter;
private RisorseList risList;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_risorse);
setTitleFromActivityLabel(R.id.title_text);
list = (ListView) findViewById(R.id.ListRisorse);
list.setClickable(true);
Intent intent = getIntent();
// vengono recuperati i parametri dalla activity chiamante
docente = (Docente) intent.getSerializableExtra(getPackageName() + ".Docente");
//cartella = (Insegnamento) intent.getSerializableExtra(pkg + ".Insegnamento");
get_response = intent.getExtras().getString("get_response");
response = intent.getExtras().getString("response");
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view,
int position, long index) {
if (risorseList.get(position).getType().equalsIgnoreCase("Folder")) {
Intent intRisorse = new Intent(getApplicationContext(), RisorseActivity.class);
intRisorse.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intRisorse.putExtra(getPackageName() + ".Docente", docente);
intRisorse.putExtra("get_response", risorseList.get(position).getURL());
startActivity(intRisorse);
} else if (risorseList.get(position).getType().equalsIgnoreCase("File")) {
//--------------- DOWNLOAD DEL FILE-------------------
}
}
});
startRisorseService();
}
// Aggiorna la View della Activity popolando la lista dei docenti trovati
private void updateView() {
this.risAdapter = new RisorseAdapter(this, this.risorseList);
if (this.risAdapter != null) {
this.list.setAdapter(this.risAdapter);
} else {
Toast.makeText(this, "Errore caricamento lista materiale",
Toast.LENGTH_LONG).show();
}
}
private void startRisorseService() {
// risList conterrà il nostro modello dei dati (l'array di oggetti
// Ricevimento)
risList = RisorseList.getInstance();
if (risList.countObservers() == 0)
risList.addObserver(this); // aggiungiamo la nostra "View" come
// osservatore del modello dati
loginState = ((LoginState) getApplicationContext());
Handler handler = new RisorseHandler();
//IT IS THE TASK THATH RETRIEVE THE DATA
RisorseService task = null;
if (response != null){
task = new RisorseService(handler, loginState, response);
}else
task = new RisorseService(handler, loginState);
if (task != null)
task.execute(get_response);
}
public void update(Observable observable, Object arg1) {
if(observable instanceof RisorseList){
// in quanto potremmo avere piu modelli dati
// verifichiamo su quale modello è avvenuto un cambiamento dei dati
// prima di effettuare il cast
this.risorseList = ((RisorseList) observable).getData();
}
updateView();
risList.deleteObserver(this);
}
public void onClickRefresh(View v) {
startRisorseService();
}
@Override
protected void onResume(){
super.onResume();
// IF I UNCOMMENT THESE LINES IT WORK BUT RETRIEVE DATA FROM INTERNET AGAIN!!!
/*if (risAdapter == null)
startRisorseService();
else{
risAdapter.notifyDataSetChanged();
}*/
if (risAdapter != null)
updateView();
}
}
答案 0 :(得分:1)
这不会回答您的问题,但您是否考虑过使用Fragments而不是每次新活动时创建?
您的想法是,您将在Fragment
中显示列表项,而不是每次打开文件夹时重新创建RisorseActivity
,您将用新的片段替换旧片段。
此外,在提交片段之前,您将调用addToBackStack()
,以便将事务添加到片段事务的后台堆栈中。此后备堆栈由活动管理,并允许用户通过按“返回”按钮返回到先前的片段状态。
这样,当您返回时,将弹出堆栈中的上一个片段,并且不会再次下载任何数据。
答案 1 :(得分:0)
请检查Saving Android Activity state using Save Instance State
基本上,您需要使用public void onSaveInstanceState(Bundle savedInstanceState)
和public void onRestoreInstanceState(Bundle savedInstanceState)
,将要保存的内容保存在Bundle(onSaveInstanceState)中并在onRestoreInstanceState中检索它们
希望这有帮助!
答案 2 :(得分:0)
在这种情况下,最好让单个实例使用单个实例在路径上保存自己的文件夹堆栈并处理按下后退按钮。它比在堆栈上有多个活动更有效。