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
callsstartActivity()
B
is shownB
shows theprogressbar
B
callsonCreate
B.DownloadFile()
is calledB
hidesprogressBar
But what actually happens is:
A
callsstartActivity()
- The screen is stuck on
A
B
callsonCreate
B.DownloadFile()
is calledB
never showsprogressBar
- The screen is stuck on
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() {
}
}