I currently have two activities in my application.
The second activity in the application prompts a notification icon to be presented in the notification bar.
The intent from the first activity to the second uses the FLAG_ACTIVITY_SINGLETOP and the FLAG_ACTIVITY_CLEAR_TOP flags in the intent. The same goes for the intent used in the PendingIntent from the notification.
My aim is that the second activity is only a single activity and only created once, then when the user leaves via the home button and then goes back to the activity via the notification, the instance that was running comes back and onCreate is NOT called.
This approach works on almost all devices, however testing on one device in particular (A Sony Xperia device - 4.0.4) has shown that it doesn't work in all cases.
On the Sony Xperia the onCreate of the second Activity is always called when it is entered into by the user, even from the notification.
I have tried using singleTop in the Manifest as well without success. Is this a bug in the device or am I doing something wrong? Perhaps missing a flag I need or something along those lines?
If anyone wants more specific code or information please let me know and I will post it.
First of all, please note that it is up to Android to kill your Activity when your app is backgrounded. So there is no guarantee that your activity will stay alive while your app is backgrounded.
In any case, in my projects I end up using singleInstance launch mode for activities that only one instance should be running at the same time.
Just try to update your AndroidManifest with singleInstance launch mode for this activity:
<activity
android:name="com.your.package.YourActivity"
android:launchMode="singleInstance"/>
Related
I have a bug in my app which I thought I knew how to resolve, but now I've thought more about what is happening I'm not sure I do know the fix.
My app receives an incoming intent from a calling third party app as a string. That string can be sent as a SEND intent or a VIEW intent.
All of that works fine unless the app is already running...
This is what is happening:
My app is not running (not listed in the running apps view)
Share is clicked in another (third party) app and my app is selected to receive the shared text (text1).
My app opens and the text is displayed (and processed) as expected.
The user switches back to the third party app and shares some different text (text2) and my app is selected to receive this new text.
My app opens, but the original text (text1) is still displayed.
At this point I thought that the bug was because I am reading the intent in onCreate() and then displaying and processing it. My thinking was that as the app is already running onCreate() is not being called when the app is shown the second time as we jump into the lifecycle at onResume().
However, if I continue the test as follows:
Without exiting my app the user switches back to the third party app again and again shares the same piece of second text (text2) with my app.
My app is displayed again but this time correctly shows and processes the second text.
How can this be, as the app is still running, surely onCreate() is still not going to be called!
I thought that the fix was going to be simply to move the getIntent() call into onResume() (or onStart() ?) But now I'm not sure about this. Is this right thing to do?
The core of the issue is the fact that your Activity is already on top of the activity stack when the second Intent is being fired.
The launch mode will matter here.
What you can do is, set the launchMode to singleTop, and then get the intent in onNewIntent() method.
That is a good way of handling such scenarios. Please read the link above and it will make things very clear.
I'm trying to create an app "hidden" from applications list.
The way i though the user will start the app is through a Receiver listening for NEW_OUTGOING_CALL and intercept a particular number dialed.
The problem is that on new Android versions, this receiver will never be activated if the app never start once. (Starting the application from a BroadCastReceiver (NEW_OUTGOING_CALL doesn't always work)).
I can't figure out a workaround for this problem: the app launcher is totally hidden so the user cannot never launch the app, and the receiver would never be activated if the app will never start.
Is there any other strategy or workaround for hide and launch the app with some kind of secret action?
Create a activity with manifest file pointing it as the the Launcher activity and make it transparent and call its finish method in onCreate. User clicking on that icon will have no idea that the activity is opened. But why don't you show the About application kind of screen in the launcher activity?
How to start an application that has no launcher activity?
Story behind the problem:
I have an application that is basically a BroadcastReceiver that waits for a couple system intents like BOOT_COMPLETED. The problem is that as my application has no Activity, it doesn't get started and so it receives no intent.
Android 3.1 release notes mention that intent options can be overridden to start up applications but I assume it requires another active application to do so.
P.S. Write all the ways you know. ADB commands as well.
First piece of advice would be to make a very simple "Welcome to my App" Activity that could be run. Use it to show a splash screen, some advertising, or be a settings screen. That gets you around the "no Activity" problem.
As far as I know, you cannot have anything hooking into BOOT_COMPLETED until and Activity in your application has been run. So you need to have an Activity of some sort.
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
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.