0

I am trying to make an App which is going to display Some Images and Videos. So I am planning to add a splash screen of around 2seconds. After 2 seconds the user will be taken to the Main Screen of the App.

I want to start the loading of the Images, Video when then user is at the splash screen itself so that the user should wait for the least time when he is at the Main Screen.

So the loading will be started at the Splash Screen and then after two second the user will be taken to main screen irrespective of the completion of the loading. Now since this involves two activities should I use a Async task or should I Use a service with an Async Task(For the callback of completion of code) within it?

Which one would be better. Also in Android 8.0 are there any restriction in using Services?

I think using a Async Task between two screens may cause Memory leak if not coded properly.

Any help would be really grateful.

EDIT: My app is having one more feature hence cannot make the user wait in the Splash Screen till the loading is over.

Rajesh K
  • 683
  • 2
  • 9
  • 35

4 Answers4

3

It is not very good to use AsyncTask for sharing results between 2 activities, because AsyncTack created in Splash activity will be destoyed (stopped) when switched to Main activity. Better to use service in this case and Main screen will subscribe for result.

redlabrat
  • 507
  • 7
  • 14
  • Thanks For your Answer. But Will Async Task be destroyed even if it is a separate class and only started from Splash Screen. If yes then Can I make a method in the Application Class which will start the Async Taks and only call that method from the Splash Screen. Will that be fine? – Rajesh K Jul 10 '18 at 09:33
  • 1
    If activity is destroyed, than all objects will be destroyed and task stopped. When task created in Application it will continue to work after activity is destroyed. In that case you need to save results of your AsyncTask in your Application. – redlabrat Jul 10 '18 at 09:39
  • Ok. Thank You very much. Will create a method in my Application Class and then make my Splash Screen start the AsyncTask from that. And will also save the results in my Application Class. Thanks Once again. – Rajesh K Jul 10 '18 at 10:11
  • OK. Just one more thought what will happen if the Async task created from Splash Screen is destroyed when the Splash Screen is destroyed. Will any crash occur? Or will the be any other adverse effect which will be noticeable to the end user. – Rajesh K Jul 10 '18 at 10:31
  • 1
    No there will be no crash, but you can check this question for more info: https://stackoverflow.com/questions/2531336/asynctask-wont-stop-even-when-the-activity-has-destroyed – redlabrat Jul 10 '18 at 13:41
  • Thank You very much – Rajesh K Jul 10 '18 at 16:22
1

So basically you want to start the download in the splash screen and continue the download in the activity that follows. In this way, you still have to implement a loading animation. In your case, I would recommend finishing your splash screen, as soon as everything is downloaded. In that way, you don't have to download anything anymore inside the app's lifecycle.

Caspar Geerlings
  • 1,021
  • 2
  • 10
  • 21
  • 1
    Thanks fro your answer. Extremely sorry as I have not mentioned in the question that I have one other feature also. And sometime the loading in low end phone takes a lot of time hence the user might close the app. – Rajesh K Jul 10 '18 at 09:35
0

AsyncTasks continue to run even after switching to a new activity. You can try the following flow:

1. Splash screen
2. Trigger Async Task
3. Main Activity
4. Show Images/Videos

The only catch is, you will not be able to fix a time for #2 to complete to be able to start #4. This is the nature of AsyncTasks. You can workaround by using the OnPostExecute within AsyncTask.

Example: OnPostExecute call another method that will enable a button. Users can click on the button to view Images/Videos. But then, this might not be a good user experience to see some button suddenly getting enabled.

Janaaaa
  • 1,346
  • 1
  • 16
  • 34
0

In that case I would rather create some Singleton with own Handler (that works in separate Thread), that will be started at Splash screen. After Main screen will start, it should ask that Singleton about respective data or should sign himself for receiving that data.