Splash screen re-showing when launched from 3rd party app - android

I have implemented a splash screen for my app. I made the splash screen activity the launch activity. The splash activity does the startup work (loading data, etc.), and then launches the "real" main activity.
The problem is this: I am using a 3rd party app that has the ability to launch other apps. Let's say my app is already running (it is past the splash screen, and is on to the real main screen). I then press the Home button to put the app in the background. I then do something that causes the 3rd party app to launch my app. What I want is for my app to be simply restored (as it would if I had tapped the app icon from the phone's regular launcher). Instead, it launches the splash screen all over again, and my initialization code runs again, which I don't want.
Since this is a 3rd party app that is starting my app, I don't have the ability to change how it launches my app. I am assuming that it is just querying the package manager to get the launch intent and starting that. So, I need to find a way to fix this in my app itself.
Is there a way that I can find out from my splash screen's onCreate method if my "real" main activity is already running, and if so, skip the initialization code and bring the existing main activity to the foreground?

Keep a static boolean in your Application class.
When initialization is done, set it to true.
When splash screen starts, check the value of the boolean, if true, go straight to your main activity without doing any logic, and instantly finish the splash activity.
If false, then assume it's a cold start and you need to run the initialization code.
There is no way to look at the Activity stack from within an Android app, so you can't check if the Main activity is already running.
There is also no way for the 3rd party app to check if your app is running, and then launch a different activity depending on that.

Related

Different app behavior depending on the way it started - from play market or phone

The problem occurs when I start my app from play market. Steps:
move from main activity to some other activity or fragment;
collapse app;
expand app.
After that steps I'll see my main activity again with the default fragment, it forgets all steps I did before collapse. This appears only after starting the app from play market when I kill the app and restart it from the menu - everything is ok.
Neither crashes on logs nor "don't keep activities" mode exists. It doesn't depend on the first start of the app.
Well, I've localized and fixed this bug. The reason is that on some specific devices after starting from play market the app won't be registered in OS, so when I collapse and expand it, the app doesn't resume work, but OS starts new task for the app. That's why main activity appears as default (main activity launchMode is set for singleTask) - launch activity starts it and already created main activity gets onNewIntent method called.
I've fixed it with ugly hack - save somewhere (I save on App class) the flag, if MainActivity was already started and after application expanded check that flag in launch activity. If true - then finish that launch activity to prevent affection on previous main task.
Hope, I've described it clear enough to understand...

How to implement Android BroadcastReceiver

I created a sample application. In my application I have 4 Activitys and 1 BroadCastReciever that runs in background. Here I wrote there service for the purpose of when the user Unlock the screen, I run my application. Everything works fine except this:
When I run my application, I'm in my third activity. The user no longer interacts with screen so the device gets auto screen locked. Now I unlock the screen means, the application runs from the first activity. I need to run the application from the user's last interacted activity (for example, if the user interacted with the third activity, I need to show the third activity when the screen unlocks).
No problem, use SharedPreferences and store a currentActivityName. Update this when you enter a new Activity via onCreate or onStart(). This value will be preserved, even when the app shuts down. Once you detect your entry point you would just startActivity(activityName) once the app returns.
Do not finish the activity when you are moving from one activity to another activity and add WAKE_LOCK permissions in android manifest file

What's the difference between opening an app from the applications screen and the recently used apps list? (android

Can anyone tell me what the difference is between opening an app from the applications screen and opening it from that recently used apps list that pops up when you long-press the home button?
I didn't even know that recently used list existed until a friend managed to break my app by launching it from there. He tried twice and got the same force quit, but when he launched it from the applications screen it opened fine.
The error log told me that a nullPointerException occurred in the getCount method on my ArrayAdaptor for my ListView.
Anyway I just wondered if there was a difference that I need to know about and adapt my code to deal with?
AFAIK, If your application is completely shutted down, launch from applications screen and recently used apps list should have no difference, both refresh start your application and open your application's MainActivity (by stack-push your application's MainActivity into a newly created task)
However, as Android is multi-task OS, your application can be put into background in standby mode i.e. open your application then short-press home button, this is not same as press back button. If you haven't override these key pressed in your application, press back button several times with pop all your activities off from activity stack and finally kill your application, whereas press home button will bring System's HomeActivity into foreground hence flip your application (AKA. task with activity stack) into background.
Things becomes more interesting here, depend on which value your configure your activity's android:launchMode in AndroidManifest.xml, if you use standard or singleTop:
1. launch app from recently used apps list always bring your standby activity back to foreground, i.e. re-order activity stack.
2. launch app from applications screen will create a new instance of your MainActivity and open it, i.e. push a newly created MainActivity into activity stack, so now you have two instances in your application's activity stack
If you use singleTask or singleInstance:
2. launch app from applications screen will use the standby MainActivity (if exist) in your application's activity stack and re-open it, i.e. re-order activity stack.
Checkout Tasks and Back Stack to see how different configurations may affect your application's activity stack behaviour.
I believe there should be no difference. These are the lifecycle methods I typically see when pressing the home button from an activity, on android 2.3.4
onPause
onStop
then when I use either the icon or previous applications to navigate back, I see
onRestart
onStart
onResume
Now, in some cases the system will tell your activity to finish while you are away (or immediately when you return if an orientation change occurred). Then you will see onDestroy, and the following when you navigate back
onCreate
onStart
onResume
I don't think there is anything mysterious going on here. According to the Activity documentation, there are only four states that a process can be in, and both of these fall under background activity.
There shouldn't be any difference in how the activity is launched from history, apart from the fact that the launching Intent will have the FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY set.
Here's an easy way to think about it. All of your activities are launched form Intents. Holding down the home button allows you to open that activity using the last intent that launched it. This can give you some unexpected results however. For instance, if you are able to launch your activity from something special like a widget.

App launch in android

We can launch the app in two ways, 1 is form the app, clicking on device back button till we reach the android home screen and launching the app or 2nd is from the app we can click the device home button and then we can launch.
How can we differentiate these to launches? In 2nd type launch onrestart will be called, onrestart will be called in some other cases also.
I want to do something in the 2nd type of launch.
Can any one tel me how to do this...
Thanks in advance.
When it comes to what happens when the activity is started, you may want to look at the following link in the developer site.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
You may want to code based on the lifecycle of the activity rather than if the application was pushed to the background by the home key vs. by the back key. There could be different reasons the application was pushed to the background or closed. This is the expected way to handle application events.

Return to last Activity

In most Android apps, when you press the Home button to "minimize" the application and then open the application again, you will be taken to the screen you were last on in that Application.
I've read the following:
When launched via icon on the home
screen, Android will always start the
activity with the
android.intent.action.MAIN filter in
your AndroidManifest.xml, unless the
application is already running (in
which case it will obviously restore
the activity on top of the stack).
but this is not happening. When I launch the application a second time, it takes me to the main Activity. The application isn't being terminated. If I then navigate to the screen I was last on, all the data and such is there.
How do I make it do what I want? The related questions all seem to pose the same answer (the one I quoted above), but this is not holding true.
Are your activities in the same task? That is, are you starting any of your activities as FLAG_ACTIVITY_NEW_TASK? I'm not positive, but that could change on the resume behavior works.

Categories

Resources