I have two activities, but they are in different packages.
Now I call an Intent in the first activity to launch the second activity. This is working. Also I can get back with finish().
But now I kill the the app with advanced android task killer. The app disapears in the list
If I was on the second activity and then go to advanced task killer to kill the app and after that I start the App again, it launches the App on the second activity.
I have never seens such a behavior.
Also if I fire an Intent on the second screen in order to get back to the first activity and then call there System.exit() the App jumps on the second screen.
Related
Scenario:
I have an app, let's call it app A.
A opens an activity of App B, using Intent.ACTION_VIEW,
and then calls finish().
Problem: App A is still found in recent apps even though I finished its last activity. However, it's window looks black.
Question: Is it possible to detect when the "Complete action using -app-" screen is finished so I can programmatically close my app or how can I properly make sure the app is gone after going to App B?
There are several things here:
The list of recents is NOT a list of recent apps, it is a list of recent tasks. If you launch another Activity, then that Activity (even if it is from a different App) usually ends up in the same task as the Activity that started it.
If you don't want your app to show up in the list of recent tasks, then you should set android:excludeFromRecents="true" on the root Activity in the manifest of your app.
You can also launch App B in another task, by adding Intent.FLAG_ACTIVITY_NEW_TASK to the Intent you use to start App B. This will cause App B to run in a different task than your app A.
I've no idea why, when you return to the task started by App A, the screen is black.
I am starting a service in my app. On click of service I am launching an activity. The service click event works fine when we go to any app and press back button and exit the app.
But if we are in any app and then press home button and click on the service the activity is not launched. If I click it more then 2 times,it opens the activity and sometimes it opens the app also.
So i am unable to understand the difference between pressing back button and home button.
After you start an activity, if HOME key is pressed, then the current activity is stopped and its task goes into the background. The system retains the state of the activity - i.e. onSaveInstanceState will be called. If the user later resumes the task by selecting the launcher icon that began the task again, the task comes to the foreground and resumes the activity at the top of the stack.
However, if BACK key is pressed, the current activity is popped from the stack and destroyed. The assmuption is the activity is done and will not be used again. So the system does not retain the activity's state - i.e. onSaveInstanceState will not be called.
Home Task :
Pressing the Home switches you from the app to the home screen, whilst leaving your app running in the background. This is a bit like switching between windows on a Windows PC.
Except that when your phone is running low on resources like memory it will start to close apps that are running in the background, so that your phone has enough resources for what you're trying to do now. Games are often amongst the first apps the phone will "kill" to save resources as they often use a lot more memory and CPU than other apps. This is why sometimes your game is still running paused, and sometimes Android has closed it for you.
The Back button is the way to close apps so that they are actually closed.
onPause() is called in Activity A when it launches Activity B. After the back button is called in Activity B, onResume() is called in Activity A.
In case of activities their default implementation is LIFO based in stack and works like:
On Back button Pressed: finish the current activity by calling stop method.
On Home button pressed: activity is being paused and then it may either resume if come back to it, otherwise system will call stop() method of activity to save unused resources and utilize memory.
but these functions can be edited by overriding if required.
Following procedure:
Start my application, Stack: [HomeActivity]
Going to Facebook, using a deep link to get into Activity X
Pressing back button results in getting back to HomeActivity instead of Facebook
Expected
Start my application, Stack: [HomeActivity]
Going to Facebook, using a deep link to get into Activity X
Pressing back button results in getting back to Facebook App
I get the expected behavior when my application is not started at all beforehand. I see that other apps like Instagram does managed to get this working properly. So even if your application is running in the background it takes you back to the activity which issued the deep-link intent.
My activity has launchMode="singleTop", onBackPressed() is not overriden, so it calls the super class implementation.
What am I missing here to get this right?
I debugged it and onBackPressed() eventually calls finish(), yet it gets me back to my application instead of Facebook.
Add
android:taskAffinity=""
to the <activity> tag for your "deep-linked Activity" in the manifest.
What is happening is that Facebook is launching your "deep linked Activity" with Intent.FLAG_ACTIVITY_NEW_TASK (you should be able to verify this by checking the content of the Intent in your Activity in onCreate() or onNewIntent().
If your app is already running, Android brings your existing task to the foreground and launches the "deep-linked Activity" on top of that task. When you then press BACK, it just finishes your "deep linked Activity" and drops you into your existing task.
Android does this because all of your activities share the same taskAffinity, so when it needs to create a new task for your app, it will first try to find an existing task with the same affinity.
If you set the taskAffinity of your "deep linked Activity" so that it is empty, this should prevent Android from looking for an existing task to launch the Activity into. It will just create a new task and launch your "deep linked Activity" into that new task. Then, when you press BACK, your Activity is finished, and the task will become empty, so the task will be finished and it will drop you back into the previous task in the task stack (which should be Facebook, since your app was launched from there).
The reason is that the launch of Facebook starts a new task. Back always navigates up the activity stack within a task.
If you have control over the intent which launches Facebook, there are flags which control the task the activity is launched within. The default is to launch within the same task.
I suspect that Intent.FLAG_ACTIVITY_NEW_TASK is being added intentionally by the system -- so this may be by design (working as intended).
PS: This presentation will teach you all you ever need to know about android activities and tasks: http://www.slideshare.net/RanNachmany/manipulating-android-tasks-and-back-stack
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.
I have a pretty standard iPhone app that creates a series of around 7 unique Activities initialised by Intents.
However if the app crashes on the 7th Activity, the app restarts on the users phone around the 5th activity. The problem then is the info gathered from activities 1-4 is null, meaning the app is useless and the only way to get the app working again is to either continually press back or else kill the process.
Why does this behaviour occur, and is there a way to force the app to start back at the first activity when it crashes.
Your app is restarting in the activity that was beyond the crashed activity on the activity stack. You can finish all activities that are beyond your current one through calling
this.finish();
after starting the next activity.
The problem is that the user now can not press the back button to change data that was inserted in the steps before because those activities are gone.
You may have a general problem with data persistence over pause and resume cycles. Try to call your emulator or phone while you are in one of the deeper activities and then return to the app through long pressing the home button. You may see that the data from previous activities is now also empty.
Play around with this behaviour and have a look at the application life cycle documents.
This may be a way to check if data is available and if not close the activity or go back to the start activity.