Android not triggering android:parentActivityName - android

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

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.

Application restarts while navigating back to previous activity

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>

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 How to go back to Activity B, which contains an ID from A, from Activity C?

I have three activities. Activity A provides a list of groups. When you click on a group you proceed to Activity B, passing the Group ID of the clicked item, so that you could see details of that group. When you press an "Add" button on Activity B, you will proceed to Activity C.
Using the back and/or up button of the action bar, I could navigate from Activity B to Activity A without any problems. I could reload again the list items of Activity A. But when I press the back button of the action bar of Activity C to go to Activity B an error occurs because the passed group ID from Activity A is not recovered.
Please tell me what is the best thing to do. I'm a newbie.
PS: I understand that there is the hardware back button but according to the requirements a back button from the action bar is required.
You do not need to kill activities when you navigate from Activity A to Activity B and then to Activity C. Most probably, you are killing the activities and this is the reason that reference to that groupId from Activity A is no longer available.
I think, you are intenting from Activity C to B. By doing this, you have called the onCreate method of Activity B. So i will suggest you to code list this in your Activity C
#Override
public void onBackPressed() {
finish();
}
When you press the back button in the action bar, are you creating the new intent and push the activity. if so then you have to pass the id to class.
if you call finish() instead of creating the intent in the onClick listener of the back button, I think there will be no issue in recovering the id.
In your manifest file use meta-data tag like this, e.g,
<activity ...>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.org.abc.board" />
</activity>

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