0

I have two Activities. A and B. A starts B.

In B.onCreate()I call a function to download a file I need in that activity: DownloadFile().

For that purpose, B has a progressbar I hide as soon as the download is finished.

What I expected was the following progression:

  • A calls startActivity()
  • B is shown
    • B shows the progressbar
    • B calls onCreate
    • B.DownloadFile() is called
    • B hides progressBar

But what actually happens is:

  • A calls startActivity()
    • The screen is stuck on A
    • B calls onCreate
    • B.DownloadFile() is called
    • B never shows progressBar
  • B is shown, file already downloaded, hiding progresbar immediatly.

Now, when I did the same thing in the same progression but instead of downloading the file, I streamed it, it all worked fine. But this could have something to do with how the MediaPlayer handles things.

I tried switching onCreate for onStart or onResume from what I've seen on this answer. But the same thing happens.

I want to switch the activity and only after B is shown, I want the download to start, but I don't know how.

Edit

As requested, the code for B

package com.skillcademy360.lite.activities

import android.os.Bundle
import android.support.v7.app.AppCompatActivity

class DownloadActivity: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        DownloadFile()
    }

    fun DownloadFile() {
    }
}

As I said, I already tried to switch onCreate for onStart and onResume and even onPostCreate, which shouldn't be used anyway. But none of them work.

EDIT:

I realize there is some ways to work around this problem but I need a direct way. All of this is tied to a inheritance and a library, so doing things like "saving the file as a global variable" won't do.

The code I provided above behaves the same way mine does, so I need this problem exactly fixed without having a workaround using AsyncTasks or global variables.

EDIT: To clarify. I get that, when I call it in onCreate the DownloadFile() blocks the UI. However, as shown below, same thing happens when I call it in onResume. As I understand it, and as its written in the lifecycle, the visible lifecycle should start after onStart, but it doesn't seem to do that.

package com.skillcademy360.lite.activities

import android.os.Bundle
import android.support.v7.app.AppCompatActivity

class DownloadActivity: AppCompatActivity() {
    override fun onResume(savedInstanceState: Bundle?) {
        super.onResume(savedInstanceState)
        DownloadFile()
    }

    fun DownloadFile() {
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Benjamin Basmaci
  • 2,247
  • 2
  • 25
  • 46

3 Answers3

2

According to Activity Lifecycle:

onStart() called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

You can try to call your DownloadFile() method creating background Thread (so downloading couldn't block UI) in B Activity onCreate() and start to show your ProgressBar in onResume() or in onStart().

EDIT: Take a look at this post

EDIT 2: If you don't want to work with global variable then you can call your DownloadFile() method in onCreate(), show ProgressBar and then do all your stuff onPostDelayed() including dissmissing of ProgressBar

EDIT 3: You probably cannot show animated ProgressBar while you are downloading file at the same Thread since your downloading will block entire UI.

EDIT 4:

  • The real progression of your actions is next:

    A calls startActivity()

    onCreate() method is called in B

    ProgressBar is setup in onCreate() but will be shown only in onResume()

    • Method DownloadFile() started and blocked entire UI

    • Methods onStart() and onResume() weren't called because of DownloadFile() exectuing. That is why you stucked on the screen of Activity A

    DownloadFile() is finished, methods onStart() and onResume() were called. ProgressBar is dissmissed.

Oleg Golomoz
  • 502
  • 4
  • 12
  • I use my downloaded file in the onPostCreate method so it can't really do it async. Or at least it would create a large overhead. As you suggested, at "onStart" it should already be visible so I'd like to do it that way. – Benjamin Basmaci Apr 08 '19 at 12:44
  • Just create global variable of your file `object`. Download it in background `Thread` and initialize this vairable somewhere in `onSuccessDownload()`. Then use this `object` fluent after `onCreate()` – Oleg Golomoz Apr 08 '19 at 12:55
  • I also updated my answer, take a look at that post. – Oleg Golomoz Apr 08 '19 at 13:04
  • I still consider all of these `workarounds` but thats not how the activity is supposed to work, is it? I mean, I could have figured out how to do it with AsyncTask but I already have many of those and as you said yourself, `onStart` and `onResume` is supposed to be called *after* the activity is visible. I want to fix this *directly*. And I want to understand why this is happening. – Benjamin Basmaci Apr 08 '19 at 13:16
  • What do you mean under `thats not how the activity supposoed to work`? Your expectetion of following progression after `startActivity()` were wrong. Activity behaves as it should. You cannot show `ProgressBar` in `onCreate`. You can setup it there, but it will be shown in `onResume`. Calling `DownloadFile` blocks your entire UI, that is why you are stucking at previous screen until Loading is finished. – Oleg Golomoz Apr 08 '19 at 13:22
  • No. Its not about the progress bar. Its about the whole activity. The App doesn't even show a "black screen" or anything, it shows the old activity `A`. Even if I do my download in `onResume`. The "visible lifetime" is supposed to be after `onStart`, so I would expect at least the activity to switch after `onStart` is called. So, when `onResume` is reached, isn't it supposed to already be visible? – Benjamin Basmaci Apr 08 '19 at 13:31
  • But I've tried the same thing with `onResume` and that doesn't work either. Shouldn't it? – Benjamin Basmaci Apr 08 '19 at 13:32
  • If you show your `ProgressBar` in `onResume()` and then call `DownloadFile()` your screen also will be freezed. The only difference is that you will have freezed screen of `Activity B` with stopped `ProgressBar` image on it. – Oleg Golomoz Apr 08 '19 at 13:37
  • But thats exactly my problem: This does not happen. Instead of seeing a frozen `ProgressBar`, I see activity `A`. – Benjamin Basmaci Apr 08 '19 at 13:38
  • Can you provide your attempt to call it in `onResume()`? – Oleg Golomoz Apr 08 '19 at 13:40
  • Please forget the ProgressBar. Its not about the ProgressBar. Its about the Activity not being shown *at all* before the download has finished. – Benjamin Basmaci Apr 08 '19 at 13:43
  • I'd advised you to add logs into all states of `Activity Lifecycle`. Starting from `onPause()` of `Activity A` to `onStop()` of `Activity B`. It helps you and me to understand the situation – Oleg Golomoz Apr 08 '19 at 13:50
0

What you could do is to create the progress bar in the activity A, so you are sure that it is called when your intent starts. Download file in B and then only cancel it.

Can you please show some code if it is still not working like this? Whats happens if you use debug mode?

ImNeos
  • 527
  • 6
  • 17
0

You can try using AsyncTask, show your progressBar in onPreExecute() method, download your file in doInBackground() and hide the progress bar in onPostExecute(),

Than you can execute this AsyncTask from your Activity B's onCreate() method.

Hope this helps.

Ankit Dubey