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.
Related
My question is about how to make splash screen, and finish the splash screen if the MainActivity done rendered, not by timeout set.
I have been searched how to make splash screen and i have done it, but mainly they are using timeout to set when the splash screen have to closed/finish.
What i want is how to finish the splash screen when the MainActivity completely rendered, not after how many seconds to close the splash screen.
Is there any way to get something like render completed state?
I tried to code the finishSplashScreen() in the onResume() and worked as i want, but the problem is, its always trigger the finishSplashScreen() when you reopenning the MainActivity whereas i just want to trigger that function only one time when the app is openned.
Is there any way to make what i want?
Update
I found what i need, this tricks simply useful.
http://saulmm.github.io/avoding-android-cold-starts
I have been searched how to make splash screen and i have done it, but mainly they are using timeout to set when the splash screen have to closed/finish.
Then you probably have not done it right.
Hard to tell without seeing your code obviously, however this link can most likely provide you guidance on how to build a proper SplashScreen :
https://medium.com/#ssaurel/create-a-splash-screen-on-android-the-right-way-93d6fb444857
No timeout, just a very fast-loading SplashScreen (no setContentView, only theming), and the only responsibility of this SplashScreen being to launch your MainActivity.
Hope that helps.
So this is a tuff one. I have a activity that calls a class that call a http downloader which is the async task. What I want to do is have a please wait loading screen notification for the user while the activity is getting all the data from the web. I don't have a clue on how to get the loading screen to be called. Usually you would have it in the async task but my async task does not have a view. How to best go about doing this?
Thank you for any help.
My http client does have a doInBackground() so should I put the on preExecute and postExecute there? But then how would my main method which is twice removed know when its down and when its not.
What is happening is that when a button is clicked it starts the activity and starts to process all the methods, but it linger on the first activity until everything is done loading. I don't want that I want the progress dialog. It just won't show. Any thoughts on this?
NEW UPDATES
Ok so now I got this set up a bit better so now at least the progress dialog now shows up. the problem is the Async task does not stop running.
You should use an AsyncTask. It doesn't have to have a view, it's just a background process just like a thread.
Refer to this on how to use an AsyncTask properly with a ProgressDialog.
Do your methods in the doInBackground() method of the AsyncTask.
I have an animation for my splash screen and on some phones which take longer to load things, it starts mid animation.
The splash screen is really an activity that has a View, in which the animation is drawn. Is it possible to wait for the screen to load and then start the thread in the View? I mean can I somehow check if I can begin the animation and it will be shown, the whole thing
Do yourself a favor and DON'T use a splash screen. On Android guidelines Google is extremely against splash screens because they give some sort of slowish UI perception to the user, and it's not a good thing at all.
If you absolutely need to wait for some sort of data to finish loading/network query you can do it much more elegantly by letting the app load normally and give the user a loading indicator (e.g. SwipeToRefreshLayout from the v7 support lib).
Not actually answering your question but this is a much more elegant solution for a completely wrong approach for a good UX. The Animation.AnimationListener has a callback called onAnimationEnd where you can do whatever after the animation has finished.
I want to execute a piece of code (say, for example, display a Toast) every time that the app is opened. So far I have managed to do this every time the app is launched by putting the code into my MyApp.java file that extends Application.
However, if I press the homescreen or back out of the app and then go into it, the message doesn't reappear. It only does when I relaunch the app. Any idea how to do this?
EDIT:
basically im asking how to execute code everytime the whole APP is brought to foreground (this can be first time open, after another app was used, after user backed out of app, etc). Where would I place onResume code? It wouldn't be in a particular activity, would it, since I want it to apply when entire app appears in foreground, not just particular activity.
You can try writing that code in your activity's #Override-d onResume() method.
The only way to do this is,
Determine which app is currently in the foreground
.Follow this discussion for getting an idea for the best way to do it.
[Determining the current foreground application from a background task or service
Suppose, if the function name is 'getCurrentForgroundApp()',
You need a service to execute getCurrentForgroundApp(); every one second
(1-second interval is depending on your purpose, can be lower or higher).
Now, you can identify which app is running foreground in every second.
So, check if your app is the one running foreground. If true, then execute the toast or code you need.
This is how app-locker apps showing lock screen over selected apps, whenever they come to the foreground.
You have to use the onResume callback:
Android API
Example of use from previous SO question
In activity class:
#Override
protected void onResume() {
super.onResume();
//your code here
}
I'm currently following some video tutorials on Android development, and it's gone fine up until the 8th tutorial, we make a splash screen, and after 2 seconds it's supposed to go to the main application. The problem is, after 2 seconds, the screen goes black and the app crashes.
Logcat Output
I followed the tutorial exactly as he did it, the only difference with mine is that I'm developing for 4.2 instead of 2.2.
I'm also using my Galaxy Note 2 instead of an emulator to test.
I did it all as he did, and pressed play, the app opened on my device and the splash screen was there for 2 seconds (i set it to 2000) and then the spinner (which I added) freezes. The screen goes black, and then it crashes and says "Unfortunately, The Basic Series has stopped."
I've tried both sleep(2000) and Thread.sleep(2000).
Code for the AndroidManifest.xml and the main.java are here
If anyone is curious, this is the video I was following.
avoid splash screens except if u really need them to do some preparation operations..
avoid Thread sleep otherwise you will face ANR crashes (use post runnable with delay instead)
avoid reading this hint: What is your activity class name? Menu, menu, MENU or uNeM?
not so good tutorial IMHO
not honorable mention: if u really want to do it in this (btw not recommended way) check video at 8:48 and try to add catch clause and log exception.
If you need to do some initializations , use AsyncTask , or a thread that upon completion will use Activity.runOnUiThread (so that it won't crash when doing UI operations).
If you need to just show the splash screen and close it after some time , use Handler.postDelayed .
In all possible solutions , don't forget to cancel them upon onPause/onDestroy (depending on what you do/need) , so that if the user has left the app (exited or left it in the background) , it won't open up with the new activity when he resumes it (or worse , show it while it has gone to the background) .