Android splash Activity only shows up once - android

I have created a slash screen for my Android app following this tutorial. Basically it's just start a Thread in the onCreate() of the splash Activity, and wait for a short period of time before switch to the main activity.
This works fine ... only on the first time running. The splash screen only shows up once after installation or rebooting my phone. On the second time, the app just skips the splash Activity like it doesn't exists.
I suspect it has something to do with Android Activity life cycle, maybe the app doesn't really exist and stays in memory. I tried killing it by: 1) Using the "Advanced Task Manager" app, and 2) Programmatically killing it in onDestory(), but none of those worked. The splash screen still won't show up after the first time.
Can anyone help me with this? Thanks a lot,

Instead of putting it in your onCreate() you could try to put it in the onStart() method. That way it'll show for each launch not just each creation which might get around your lifecycle issue.

Related

My app is getting shut down when i try to resume after some time passed

I figured out what kind of circumstance makes this happen
First i start my app and after finishing loading i go to home screen and do something else
and after 15 minutes i reopen my app and it works fine without showing splash activity.
It seems like the app just resume so it doesn't need to load from the beginning
Weird thing happens if i wait 30 minutes before reopening my app.
When i reopen my app after 30 minutes waiting, my app shows loading page like from the beginning and then it just gets shut down before it shows main page.
I really tried to solve this problem by myself but it doesn't show the same result if i use android studio for debugging. It just works fine and it never shows first activity no matter how long i wait it just enters to the main activity right away.
So i really don't know where to start looking for to solve this problem
Firstly, apps don't stay in your RAM forever. Sometime it will eventually be turned off by the memory management. It will have to restart from the starting activity.
Secondly, you have not posted code of your starting activity so that we can diagnose whats wrong. You can post the code here.
But I am not clear of your question. You say app directly goes into the main activity [assuming your first activity was a splash screen], looks like it still in RAM.
I would suggest you to paste the code here so that I can test further.
Your app loads directly into main activity sometimes but crashes the other times. That's awkward.
The case might differ on what's usage of RAM at that time and what other apps are on.

prevent multiple instances of android app from opening

I've done a fair bit of searching for my issue, but can't find any solutions to my exact problem.
I have an app that has 3 activities in succession. The main activity (on launch), the second activity (where most of the meat and guts live), and then a final activity that is more or less a summary page of what happens in the 2nd activity.
I run into this issue semi-sporadically where if I keep the 2nd activity running in the background (turn the screen of my phone off, or just hit home) and then hit the launcher icon to go back to the app, it creates a new instance instead of returning to my 2nd activity that is running in the background. I can't find any rhyme or reason as to why it happens sometimes and not others (usually it doesn't). Most of the time it takes me right back to my 2nd activity and all is good in the world, but on occasion, it fires up another instance.
Is there anyway to check if an instance is running and ALWAYS return to that instance if there is?
When you hit Home or the screen turns off, the app automatically goes through the onStop() phase. Depending upon the available memory, the app might become a candidate to be killed by the Android OS. There really is no way to prevent the OS from killing an app that is no longer in the foreground - this is an Android OS design. This should most likely not happen if you do not have a lot of applications open.
If the first instance of the app is still around and you launch the app, it will automatically retrieve this instance. Android is designed to never create a second instance of the same app while the first one is still running. Hence there is no way to check is an instance is running. You could use logging inside onDestroy() to see when the app terminates. But again, Android does not always call onDestroy() and might just simply terminate the app.

onCreate() called while Activity is stopped (but not destroyed). Only after installation

I'm working on an app that targets Api 19, and that is basically a Processing sketch.
The problem I'm facing is that the first time my app is run, right after installing it, it works well until the user sends it to the background. Then, if they click on the app icon again, onCreate() is called but the activity is not destroyed or restarted. Some variables change and that produces strange behaviours.
This happens ONLY the first time the app is used. After force closing it, this behaviour won't happen ever again (as far as I've tested). And this does not happen when launching the app from Eclipse either.
Summarising, this is what happens after the first force close (and what I deem correct):
Activity is running.
Activity is sent to back via home button
onPause()
We click on the app icon again
onResume()
And this is what happens -ONLY- the first time the app is run after installation:
Activity is running.
Activity is sent to back via home button
onPause()
We click on the app icon again
onCreate() <-- !! note no onDestroy()
onResume()
I wonder if the fact that I'm using Immersive Mode has anything to do with this, but changing the Api target version to 10, removing Immersive mode or testing on old devices do not help. I have, of course, used android:configChanges="orientation|keyboardHidden|screenSize" on my manifest.
Does anyone have a clue on what might be causing this? Is this a common issue or should I look for a bug in my code? Maybe a Processing bug?
Thanks in advance for any clue. I hope this is the right way to ask about this issue. This is my first post.
Update:
My explanation is not very accurate, but apparently there is a bug report for this. The problem is much better explained here: https://code.google.com/p/android/issues/detail?id=26658
Unfortunately, I can't get the proposed solutions to work, using this in onCreate() causes my app to either close or crash:
if (!isTaskRoot()) {
finish();
return;
}
Ok, this is how I solved it, in case anyone else bumps into this wall.
This might affect especially people coming from the Processing Developing Environment, as it converts a "Processing sketch" into the only activity of the project.
The original problem (Android managing apps in a different -wrong?- way when launching them from the Package Installer) is well explained here: https://code.google.com/p/android/issues/detail?id=26658
Solutions posted there might solve most cases,but if -like me- your launcher activity is the one that carries out all the work, you will need to create a specific launcher activity that just starts the main one... and commits suicide when the Android bug happens.
Add this bit to the onCreate() method of the launcher activity:
if (!isTaskRoot()) {
finish();
return;
}
I hope this helps.
This looks like a valid application lifecycle, you put your app to background, android is then allowed to destroy your app. onDestroy is not guaranteed to be called, you have to be prepared for that, as you say onPause is called so you can use it. Why it happens only once after install is hard to explain, but in my opinion you should not actually care about it and prepare your app to be killed any time it is in background.
In above diagram may OnDestroy method never called because other application need memory.
onStop() is the last method which is guarantied to be called. After this method Android is allowed to kill your activity at any time. Check out the table in the activity's lifecycle table. There is a "Killable" column describing the time, when activity can be killed.
If you don't use static variables and you initialize everything properly in onCreate(), then you should have no issues with this behavior. Just never expect onDestroy() to be called.

How to kill an An Android Application Programatically

I am working on Android applications and getting one problem. I am using Exit Button, so when I am clicking the exit button (which is present in last activity of the application or in Middle but not in First Activity) then the application should close, and when I am restarting the application it should start from the first screen.
But my problem is it's not killing the application and when I am starting again it is starting from the previously paused activities.
I have used System.exit(0) and also used android.os.Process.killProcess(android.os.Process.myPid()).
but those are not working.
in an Activity: this.finish()
in a Service: this.stopSelf()
in a Receiver: return;
You should not. It goes against basic android ideas. Please read about applciation lifecycle.
You could finish individual activity / service with finish()
System.exit(0) will do the job. However, it is not at all recommended. Android does some caching, and saving state, so as to relaunch your appp quicker, the next time you launch it.. so you do not want to be messing around with that.
This has been discussed many times, please research your question before posting.

how to completely get rid of an activity's GUI (avoid a black screen)

I'm trying to write a very simple app that will do just one very simple non-GUI action, then display a short message (using toast, on top of what was already on the screen, e.g. home screen), and finish. That's all.
This almost works, but i'm still getting a brief black screen after the app starts, then back to home screen and things work as i intend after that. I thought that not using setContentView on my activity (thus depriving it of a View) would be enough to avoid the black screen, but it still pops up.
Please notice that (as suggested in a related question), a service is not the answer to my problem. I just want a GUI-less (except for one toast) app that runs and ends right away.
Thanks :)
In <activity> in your manifest use
android:theme="#android:style/Theme.NoDisplay"
Note this assumes you will call finish() before returning from onResume(). If you are going to do more work after that (for example if you will be doing any networking or other such thing that can't be done quickly synchronously), you will probably want to start a service to take care of the work to tell the platform your process should continue running after the activity finishes.
Sounds like a job for an IntentService.
You send it an intent, and if it isn't running it starts, then it treats all received intents, then it stops. Short and simple.
a service is not the answer to my
problem
Yes it is

Categories

Resources