How to implement Android BroadcastReceiver - android

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

Related

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

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.

Android - onCreate() called despite using SingleTop launch mode

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"/>

How to stop calling onCreate again when launching application in kindle fire device?

I developed an android application with two screens. The application is working with android devices good. But i installed application in amazon kindle fire tablet launched the application started good. I navigated from 1st screen to 2nd screen and pressed home button.
And again i launched application from applications menu. application is showing 1st screen instead of 2nd screen. Common behavior of android is should show the second screen.
What i know is while press on home button the application will go for onPause() state and launch application form launcher icon it will go fore onResume and show the screen where it has previously.
But application every time calling onCreate when launching from launcher icon. This happened in Amazon kinlde fire tablet only.
I am starting the 2nd activity from 1st activity using startActivity(intent); method
May i need to use any flag for kindle fire tablet.
Please suggest me as ASAP.
Thanks in advance.
Android doesn't guarantee that once you press back from an activity, it will remain in the onPause state. It depends on how aggressive the memory killer is, or how low the device memory is. What this means is, don't count on your application being in onPause state ever.
What you could do is, save the activity state in a shared preference, and let the Application object launch the corresponding activity, restoring UI state if required.

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.

Categories

Resources