0

I have an Activity(A) which is downloading a file and putting the data(some book titles) from it into a listView. And from this Activity(A) i starter another Activity(B) which i can do some work with it. The problem is when i wanna back to (A) calling this.finish() using a button(from B), the file starts downloading again(although it has been downloaded). I am using a DownloadManagerto download the file and a String[] book to put the contents of the downloading file. I tried to save my Activity State, like this(i don't want the file to be downloaded again):

protected void onSaveInstanceState(Bundle state) {
   DownloadManager downloadManager = null;
   super.onSaveInstanceState(state);
   state.putStringArray("message", book);
   state.putParcelable("message1", (Parcelable) downloadManager);
}

And to recreate my Activity, i tried this(i don't want the file to be downloaded again):

public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (wifiManager.setWifiEnabled(true)) {
        book= savedInstanceState.getStringArray("message");
        downloadReference = savedInstanceState.getParcelable("message1");
    }
}

But the app crash when i wanna back from B to A. Any help or hints to show me how i can go from Bto A without downloading the file each time is very welcome. Thanks in advance, Carl.

Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
carl
  • 388
  • 2
  • 19
  • where you are writing this.finish() in which activity? – Hanuman May 14 '15 at 14:47
  • in `Activity B`. I'm using a button `close` to close `B` and back to `A`. – carl May 14 '15 at 14:52
  • hey just see this link you will get the solution http://stackoverflow.com/questions/4038479/android-go-back-to-previous-activity – Hanuman May 14 '15 at 14:55
  • i tried this: `Intent intent = new Intent(A.this, B.class); startActivityForResult(intent, 0);` . But from `B` to `A` using just `finish()` i get the same problem (file being downloaded..)!! – carl May 14 '15 at 15:21
  • when you are downloading file and is it changing activity or not just check – Hanuman May 14 '15 at 15:24
  • this is my onCreate(): `protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loadViews(); connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE); wifiStatus(); }` - `wifiStatus()`method check if wifi is `ON` --> `downloadFile()`, if `OFF`: wifi `ON` and `downloadFile()`. – carl May 14 '15 at 15:32
  • `startActivityForResult` waits for callbacks when the started activity decided to finish. I'm not waiting something from the `child Activity`. I wanna just save the state of `A` when i go to `B`. And when i wanna back to `A` i find my saved state(without downloading again the file...) – carl May 14 '15 at 15:39

0 Answers0