Android Navigation state perservation - android

Hi I'm working on a android app where I'm navigating from Activity A to Activity B and then to Activity C then to Activity D
Each Activity will pass some value to the next Activity and the Activity will use the value to setup the view. (i.e user id, project id).
In the manifest.xml I'm using android:parentActivityName and Meta-data android.support.PARENT_ACTIVITY to setup the back button on the Action bar.
When I press the back button on Activity D, Activity C is recreated and the intent passed to Activity C is lost. I know I can use android:launchMode="singleTop" to fix that. But If i navigation from Activity D to Activity B, is there a way to preserve the value Activity received from Activity A?

You can use Application Singleton class in android. It maintains global application state.
Please refer my answer here

Related

How to initialize an activity from another activity

I have activities A and B. Activity B is started by a service while the application is closed and activity A is not on the stack. Activity A has members that B requires access to. I would also like the home button to open activity A from Activity B even if activity B is started from a service. Is there a way to inject activity A back into the stack?
In your Service start ActivityA not ActivityB and pass some parameter to ActivityA.. and in ActivityA's OnCraete method launch the ActivityB.. now you have both in the stack.
You can declare the logical parent of each activity in your manifest file, using the android:parentActivityName attribute (and corresponding element),for home button working.
And to add a activity back to stack
Check this link for more details.
That won't be a good decision, better use the members in the B Activity and Once Activity B is launched and it's about to leave just give intent to Activity A.
Also you can check if Activity A is already in the stack. If already in the stack, it will popup by itself, if not found, can pro grammatically trigger it.

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

Launch existing singleTask Activity from launcher icon

Scenario:
Activity A (MAIN and LAUNCHER in manifest) starts up when clicking on launcher icon.
In turn it launches Activity B.
Activity B then launches our main app Activity C (MAIN and singleTask in manifest).
Behaviour I require:
Once Activity C has been shown and the home key is then pressed, the next time the launcher icon is pressed I would like to skip straight to Activity C (and not show Activity A (and consequently B) again).
I have tried using FLAG_ACTIVITY_CLEAR_TOP from A, but I still get Activity A whenever I hit icon on launch screen.
Is appearance of my singletask Activity C from launcher achievable?
Update: Use of FLAG_ACTIVITY_CLEAR_TOP from A and not calling finish() creates the situation whereby Activity B appears on press of launcher icon. However, also applying use of FLAG_ACTIVITY_CLEAR_TOP from B and not calling finish() does not resolve situation. now I don't get A on launcher icon press, but get B. What gives!
See similar scenario here.
In your case, I would recommend using a similar approach, where you would have a SharedPreference that would persist a value representing whether your app had previously been launched.
Create a simple Activity that will contain the logic for what you are trying to do here. In that activity's ("pre-A" Activity) onResume() method, check the value of the preference representing whether the app has ran previously.
If it has not previously been ran, launch an intent to Activity A, which calls the subsequent B and C activities; otherwise, launch an intent to Activity C.
Seems pretty straightforward, just remember to define the new "Pre-A" activity in your manifest!

android:parentActivityName vs onBackPressed startActivity

In Android application, does somebody know
what is the difference between :
override onBackPressed in an Activity and startActivity
vs
put "android:parentActivityName" in manifest in the activity tag
Thanks
android:parentActivityName :
The system reads this attribute to determine which activity should be
started when the use presses the Up button in the action bar. The
system can also use this information to synthesize a back stack of
activities with TaskStackBuilder.
This attribute was introduced in API Level 16.
means if you have three Activities A,B and C in your Appliction.you have set android:parentActivityName=".A" for Activity C in Manifast
when you start Activity B from Activity A and C from Activity B.then user Press back button from activity C.user automatic go to Activity A instead
of Activity B.
onBackPressed :
Called when the activity has detected the user's press of the back
key. The default implementation simply finishes the current activity,
but you can override this to do whatever you want.
called when user press Back key from any Activity. onBackPressed finish Current Activity and resume Previus one.for example
if you start Activity B from Activity A and Activity C from Activity B. if user press back button from Activity C then
System finish Current Activity C and Resume B.

Categories

Resources