Using splash screen as a loader - java

I want to use my a custom splash screen whenever my app comes back from background and i want the splash to be displayed until all data from the app is loaded. so actually the splash will be working as a loader.
Currently I have implemented DefaultLifecyleObserver to my Main class and on its onStart() method I start a new activity which i am using as a splash. In splash activity onCreate method i am doing nothing but calling finish() method after 3 seconds.
so basically whenever app returns from background it redirects to splash screen and after showing that for 3 seconds it shows me where i left.
Problem with it is that when app loads from background it shows where i left for 1 second and then it redirects to splash instead of instantly redirecting me to splash.
i have tried using onRestart onResume methods it doesnt work with my requirements too.

Related

Activity starts but I need to minimize to see it

I have a splash screen that is an activity, after 2 seconds it will launch the login activity but the screen remains the same. If I change to home screen and go back to app, the screen updates to the login activity. This is happening recently, before I didn't see this issue. Also I haven't touched anything from the splash screen.
I'm using a Handler with postDelayed.
handler.postDelayed(runnable, 2000);
you are going to splash the screen to the next activity when you write the finish() because you don't want to again open this activity when the user press back press.
The same thing is the log-in screen when the user enters your login details in this time you enter the boolean value in the Sharepreference variable and also finish() login activity.
The next time the user opens the app splash screen will be visible when the splash screen visible you decide the activity through share preference variable data where you are going.

Loading Images,Videos in background

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.
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.
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.
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.
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.

Start Splash Screen after initial App intro launch

I implemented an app intro which when first launched, takes the user through an introduction of the application.
This app intro only shows at the initial first launch of the application and ceases afterward. Is there a way I can implement my splashscreen to where the splashscreen will launch for subsequent launches?
Here're my recommendation:
Always start your Splash screen as main Activity.
In onResume() method of Splash screen, look for a boolean in your
SharedPreferences against a key (let say "isFirstLaunch")
indicating if the into has been shown before or not. If not, this is
your first launch.
Now immediately launch your Intro Activity if its first launch from
onResume() of Splash activity, and override Intro Activity's
onBackPressed() or onDestroy() or finish() or inside your
custom button click listener to save the boolean into
SharedPreferences indicating it has been shown.
For every other launch, continue your normal flow of Splash.
This way, managing from Splash screen is easy.

Dialog Fragment Not Displaying on Launch

So I have some application, on startup it launches a splash screen activity and checks to see if a user account has been linked to the app. If one has the app launches the main activity and bypasses the splash silently. If there is no user account it pulls down a database in a AsyncTask and asks the user to Login/Create an Account.
On first startup the app syncs with a server and pulls down a database. I'm trying to get a DialogFragment with an indeterminate progress bar to display as this database is pulled down. Right now the DialogFragment is created in the onPreExecute() of the AsyncTask, the AsyncTask is executed in the Activity's onStart() method the problem I'm having is that the dialog is not being drawn to the screen.
I have debug logging that shows that the dialog's onCreate() is executing and the sync is successful. The dialog is shown/dismissed in onPreExecute() and onPostExecute() respectively both of which execute on the UI thread. I realize that if the database download is fast this won't display very long but I'm not seeing even a brief flash of the dialog in the emulator.
Could this be a 'problem' with the emulator where all the dialog's frames are being skipped or am I not creating the dialog at a point where it would actually be drawn to the screen.
From your description I see 2 possible reasons:
1. Maybe you doesn't show right the DialogFragment, see example.
2. The AsyncTask is launched in a different thread (Not on UIThread), so if you need to access the UIThread (from the AsuncTask), try to override onProgressUpdate see tutorial.
Hope it was helpfull.

Android - Activity behaviour?

I have a small Android application with a TabScreen as my main screen. I have a small problem where my application currently loses focus on the last Activity the user was on.
This is the scenario:
User launches application
Application shows login screen
User enters login details and goes to tab screen
User leaves application via home key
User presses application icon to return to app and the login screen displays again
I want the application to return to the last known displayed Activity in this case.
What I do at the minute is launch the login screen as the Main/Launcher Actvitiy, then when correct credentials are entered launch the tab screen activity and finish the Login activity.
Is there a launch mode or something I should be using to achieve this?
EDIT: More info
The Tab screen is launched simply like this:
Intent intentTabActivity = new Intent(getApplicationContext(), TabScreenActivity.class);
startActivity(intentTabActivity);
Leaving the application through the home button.
I intend to persist the login state and bypass the login but on smaller applications I have created the application returns to the last displayed activity automatically and does not return to the initial Launcher screen every time and I was wondering why this is not the same behavior in this application.
Also as per my other question HERE the behavior seems to be different for debug and signed releases.
This has always been tested on real devices.
This is the correct behavior. Essentially what happens is as soon as the activity goes in the background it is on the mercy of Android DVM. If DVM feels it needs space it will essentially go ahead and kill your application. So once you try to start the application from the icon it actually restarts it from scratch.
However to solve your problem, you should have a checkbox like "Automatically login" or "Remember password" on the login screen and when the user checks it everytime the app opens it should automatically log you in and take to the next screen. This behavior needs to be implemented by you using some sort of persistent storage.
Might be because you are using your onPause() so that your tabs does some action when the tab is passed.
So eventually when the home key is pressed onPause() will be called which might lead you to this problem. Maybe you will have to check your onPause() for this case.
on home button only onStop is called, are you doing finish of activity on onStop? If not it preserves what was the activity which is top of the stack.

Categories

Resources