2011-11-26 8 views
5

私は、ファイルシステム(フォルダごとに1つのアクティビティ)を横断する際に、アクティビティの複数のインスタンスを再帰的に作成するアプリケーションを用意しています。アクティビティスタック内を前進するとき(つまり、通常のフォルダトラバーサル)に、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

Saving Android Activity state using Save Instance State

基本的に確認してください、あなたがバンドル(onSaveInstanceState)に保存し、onRestoreInstanceState

でそれらを取得したいものを保存し、 public void onSaveInstanceState(Bundle savedInstanceState)public void onRestoreInstanceState(Bundle savedInstanceState)を使用したいと思うでしょう

希望します。

1

これはあなたの質問には答えませんが、新しいアクティビティを毎回作成するのではなく、Fragmentsを使用することを検討しましたか?

リストアイテムをFragmentに表示し、フォルダを開くたびにRisorseActivityを再作成する代わりに、古いフラグメントを新しいものに置き換えます。

フラグメントをコミットする前に、フラグメントトランザクションのバックスタックにトランザクションを追加するために、addToBackStack()を呼び出します。このバック・スタックはアクティビティーによって管理され、ユーザーは「戻る」ボタンを押すことにより、前のフラグメント状態に戻ることができます。

このようにして戻ると、スタックの前の断片がポップアップし、再びデータはダウンロードされません。

0

この場合、パス上にフォルダのスタックを保存し、戻るボタンの操作を処理する1つのインスタンスで1つのアクティビティを持つことをお勧めします。スタック上に複数のアクティビティを持つよりも効率的です。

関連する問題