-
Notifications
You must be signed in to change notification settings - Fork 395
/
DownloadActivity.java
63 lines (51 loc) · 2.23 KB
/
DownloadActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.google.firebase.referencecode.storage;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FileDownloadTask;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import java.util.List;
public class DownloadActivity extends AppCompatActivity {
// mStorageRef was previously used to transfer data.
private StorageReference mStorageRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lifecycle_demo);
}
// [START storage_download_lifecycle]
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// If there's a download in progress, save the reference so you can query it later
if (mStorageRef != null) {
outState.putString("reference", mStorageRef.toString());
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// If there was a download in progress, get its reference and create a new StorageReference
final String stringRef = savedInstanceState.getString("reference");
if (stringRef == null) {
return;
}
mStorageRef = FirebaseStorage.getInstance().getReferenceFromUrl(stringRef);
// Find all DownloadTasks under this StorageReference (in this example, there should be one)
List<FileDownloadTask> tasks = mStorageRef.getActiveDownloadTasks();
if (tasks.size() > 0) {
// Get the task monitoring the download
FileDownloadTask task = tasks.get(0);
// Add new listeners to the task using an Activity scope
task.addOnSuccessListener(this, new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot state) {
// Success!
// ...
}
});
}
}
// [END storage_download_lifecycle]
}