Application restarts while navigating back to previous activity - android

See I have 3 activities, A, B and C. I go to activity C from A.
A -> B -> C
While in activity C, press home button and leave it for some long time. When I bring application back to foreground after some long inactivity either from application back stack or from launcher icon, I land on activity C. Now, while in activity C, if I press back button or click back navigation arrow from action bar, application restarts and starts activity A instead of resuming activity B.
What could possibly cause this behaviour?

Check whether you doing like this or not
<activity
android:name=".C"
android:parentActivityName=".B">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".B" />
</activity>

Related

Add Activity on top of existing backstack when launched from notification

I have Activity A, B and C
C is launched from notification.
But, backstack gets cleared when launched from notifications. I know about TaskStackBuilder and specifying the back intents, but that will be a hardcoded back stack.
If C is launched after A, then back press should go back to A
If C is launched after A>B, then back press should go back to B and then got back to A on second back press.
How do I preserve the current back stack and add on top of it?
Set all the possible target activities' launchMode in your Manifest.xml to either "singleTop" or "singleTask" depending of your need:
<activity
android:name=".YourActivity"
android:launchMode="singleTop">
The different launch modes are well explained here.

Android not triggering android:parentActivityName

Activity A is the main launcher Activity for my app
A starts Activity B upon some condition
Activity C is the Settings Activity
Android Manifest- C is the parent of B, as shown below:
<activity
android:name=".view.ActivityB"
android:label="#string/title_activity_favorite"
android:parentActivityName=".view.ActivityC">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".view.ActivityC" />
</activity>
Flow:
I start the app, Activity A is called
A starts B
When I click on
back button in the Status Bar, I am routed to A not C. Any clue why
is this is happening
You have a problem understanding how activities are started and finished. Having your example, if activity A starts the activity B it doesn't mean that the system is creating the activity C (simply because is the parent of B) to insert it between A and B, and this is why when back is pressed the activity A is resumed. To add an activity C between A and B without explicitly creating it read this post about creating a proper back navigation. If you have any problems reply with a comment.
Here is the confusion:
The android Back Button is calling (popping) the top of Back Stack so if not manually manipulated, it contains the last called activity. In your case Activity A is called just before Activity B so the top of the Back Stack is Activity A
But there is another soft back button in the android as displayed in the following image:
it is usually displayed in the top left corner of the screen. The behavior of this button is what is described in your Manifest. So if you are in Activity B and you press this button you will go to Activity C instead of Activity A
You can manually change the behavior of the Back Button with TaskStackBuilder

Android activity restarts wrong Activity after system "kills"app

I have an app with several activities. I enter my app, go from activity A > B > C. Go to home screen. I click the launcher icon, it resumes activity C. All good so far. However, if I repeat this, but then open 10/15 apps, and try it again, Android restarts activity A with a backstack of C, B, A. Clicking back should go home, but it resumes activity C.
I've tried singleTop and standard launch modes for the main activity. No help!

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.

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