Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Going back in a recursive activity (FileSystem like)

I have an app that creates recursively multiple instance of an activity as it traverses a file system (one activity per folder). I retrieve data via Intent when moving forward in the activity stack (i.e. normal folder traversal). The apps runs well when i go from parent folder to child folder. I have problems going [back], because list of files/folders remains the same of the previous activity (the child). I don't want to retrieve again folder data from internet, so it' possible to keep track of previous activity state? Here's a piece of code of my activity. Thanks

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

}

like image 806
Giovanni Avatar asked Nov 14 '22 13:11

Giovanni


1 Answers

This won't answer your question, but have you considered using Fragments instead of creating every time a new activity?

The idea is that you would display the list items in a Fragment, and instead of recreating the RisorseActivity every time a folder is opened, you would replace the old fragment with new one.

Also, before commiting the fragment, you would call addToBackStack(), in order to add the transaction to a back stack of fragment transactions. This back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button.

This way, when you'll go back, the previous fragment from the stack will pop up, and no data will be downloaded again.

like image 122
Andy Res Avatar answered Nov 16 '22 04:11

Andy Res