Android- noHistory = true not working when come back from recents - android

I'm following the next flow, Start activity A (loading resources), then go to Activity B (login). I marked Activity A as noHistory = "true" and just after I start the activity B I call finish() on activity A.
The app works as expected and when I pressed the back button from the Activity B the app closes, when I pressed the recents button and select my app, it restarts from activity A, I want it to starts from activity B.
thanks.

Add this to your activity declaration in the AndroidManifest:
android:excludeFromRecents="true"
And it will be excluded from the "recent apps" list

that is expected behaviour . Since you have pressed the back button the app is closed , if you launch from recent after that its like clicking on app icon to launch the app . You can use shared preferences to save if the first activity is already launched you can directly launch second activity

Related

Activity launchmode and lifecycle

I have two activities A & B. in A, i am showing a list of titles, and on clicking a title, it will open the detailed article in activity B.
I declare A as singleinstance in Manifest.
But if I declare A as single instance, and when the Activity B is opened and paused, then Activity A is not available on backstack.
I will try to explain by reproducing:
Activity A (launchMode = SingleInstance) with list of titles.
On clicking a title, Activity B opens
On clicking back button/up navigation, Acitiviy B finishes and Activity A resumes.
Again open activity B.
Press home button of device (Activity B goes to background - onPause)
Activity B is available in Recent Apps
Open app from recent apps/launcer - Activity B opens
Clicking back button/up navigation - Act B finishes, but Act A not resumed.
How can I provide better up navigation?
For my idea, you can change lauchmode of Activity A and Activity B from Singleinstance to android:launchMode="singleTop". I work fine for me. It is well said here. Let try.
When you will press the home button the activity B would call onStop() method. For a better understanding you can refer to https://developer.android.com/guide/components/tasks-and-back-stack.html

How to resume activity when application relaunched from android launcher?

I have an application that implements two activities. Activity A is for selecting some files on device and activity B is to show additional info while these files are processing. Both of them have singleInstance as launch mode.
On application's start activity A runs. Then this activity starts second activity B, that creates a notification. If I tap this notification or open running app from recents, it works fine and shows running activity B. But if I'd started application before and activity B is already running, launching it again from app menu causes showing activity A when old activity B is already running and accesible from notification bar.
So, what should I do to make application run only single activity at the same time and show second activity when called from launcher (if second activity once started and isn't finished)?
Your launcher intent should be mapped to a dummy activity, with no view (or may be a splash who knows !). In onCreate of that dummy activity you check if a state was saved previously and based on the state switch to the desired activity.
And how to set the state ? well lets try something simple. lets say when Activity A resumes we put "A" to some key in SharedPreference. if Activity B resumes we put "B" to the same key in preferences. Now your application goes out when Activity B was visible, the state saved would be "B", you launch from home screen, the dummy activity finds "B" in the state and launch intent to go to B else go to A.

Relationship between android:noHistory and android:finishOnTaskLaunch

How do these two attributes relate? If I have android:noHistory="true", does having android:finishOnTaskLaunch="true" have any significance/meaning?
Let's say you have three activities in your app: A, B, and C.
You start your app and see A, click a button and see B, click a button and see C.
First scenario
Now if you press the Back button on your phone, you will see B.
Second scenario
Let's say that B has android:noHistory="true".
Now if you press the Back button on your phone, you will see A. The android:noHistory="true" attribute removed B from the history (i.e. the activity stack), so you will not see it when you hit the Back button.
Third scenario
Let's say that C has android:finishOnTaskLaunch="true".
Now if you press the Home button on your phone and then launch the app again, you will see B. Android ended C when you launched the app again because it has the android:finishOnTaskLaunch="true" attribute.
finishOnTaskLaunch would kill the Activity as you move to another task. But noHistory would kill the Activity if you move to another activity in the same task.

How to make launch activity always as home activity

I have 3 activities in my app. A is the launcher activity, when I press on a button Activity B will be started, then I have a button to start Activity A or I can press back button and I can go to A. On back press previous values retained in and on button click new values will be set on A.
When I click a button in B , A will be started, I am using REORDER TO FRONT flag and singletop as launch mode. New activity is not getting created . When I press back button on A it will transit to B and again I press back button app exits. I want to have activity A on back press on B. I cannot do anything in onBackpressed() in B as B is used in several scenarios apart from the above mentioned scenario. How to manage it.
I have nor clearly understood what you are trying to do, but you could try the up-navigation pattern (http://developer.android.com/design/patterns/navigation.html#up-vs-back).
You would have to declare in your manifest that activity A is the parent activity of B and then use NavUtils.navigateUpFromSameTask(this); in your button OnClickListener.
(NavUtils is in the support.v4 package).

Android Activity back stack and multitasking support

I have an app that supports multitasking (working in the background), however I have run into problems with the android backstack.
This is what I have:
Activity A starts Activity B for result, so...
Activity A --> Activity B
If when at Activity B the user long presses the home button and switches to another application (say the browser for example) and then long presses the home button again and comes back to my app, they will be at Activity B, however the back stack at this time will look like this:
Activity A --> Internet Browser --> Activity B
So when I do finish() to send back a result from my Activity B it does not come back to my Activity A, but rather to the Internet Browser...
This is also the case if the user doesn't use long press of the home button, but also uses the home button to come back to their launcher and then uses long press home button to come back to my app. In this case the back stack is even worse:
Home Launcher --> Activity B
So when I do finish() on Activity B, the user gets back to their home screen and they can never get back to Activity A except for if they go and start the app again from their app drawer.
Is there any way to implement multitasking work in this case? Activity B needs to always return back a result to Activity A no matter what the user opened in-between these two.
OK. After long hours of research and trying various things, here's the solution to the problem. Hopefully this helps others...
The solution is pretty straight forward and simple, in AndroidManifest.xml
set android:launchMode="singleTask" for Activity A
set android:noHistory="true" for Activity B
This way Activity B will be removed from the Stack if we go to another app like the browser or exit to the home screen, so when we come back to our app we get back to Activity A.

Categories

Resources