I have a side menu drawer in my activity that has 2 options ("My files" and "Sync"), each of which is a fragment. When I am in "Sync" fragment, there is a button to start downloading files from the server. This is done through an intent service which is running in the background. I use a result receiver in my fragment which keeps getting the download progress (%) from the service and displays it in the fragment.
The problem is that if I switch to the "My Files" fragment while the download is going on and come back to the "Sync" fragment, the view is reset and the progress is lost. The service keeps running in the background but the fragment does not show the progress.
My question is that when I switch the fragment, does the "Sync" fragment still receive the progress from the service that keeps running in the background. How do I start receiving the progress updates from the service when I go back to the "Sync" fragment.
Below is the code in the fragment that starts the service.
intent.putExtra("link", downloadLink);
syncReceiver = new SyncReceiver(new Handler());
intent.putExtra("result_receiver", syncReceiver);
getContext().startService(intent);
Code in the service that sends the download progress to the fragment.
resultReceiver = intent.getParcelableExtra("result_receiver");
link = intent.getStringExtra("link");
while ((bytesRead = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
byteCount += bytesRead;
String kbs = String.valueOf(byteCount / 1024);
bundle = new Bundle();
bundle.putString("response", kbs);
bundle.putString("total_data", total_file_size);
resultReceiver.send(CONTENT_PROGRESS, bundle);
}
The progress receiving code in the fragment.
public class SyncReceiver extends ResultReceiver {
private static final int CONTENT_PROGRESS = 2;
public SyncReceiver(Handler handler) {
super(handler);
}
public void onReceiveResult(int resultCode, Bundle data) {
super.onReceiveResult(resultCode, data);
String response = data.getString("response");
if (resultCode == CONTENT_PROGRESS) {
updateContentProgress(response, data.getString("total_file_size");
}
}
}
private void updateContentProgress(String progress, String total_file_size) {
double current = Double.parseDouble(progress);
double totalData = 0;
totalData = Double.parseDouble(total_file_size);
String percent = String.valueOf((current / totalData) * 100);
status.setText(R.string.download_progress);
status.append(" " + percent + "%");
}