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.
Related
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.
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
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
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).
Example: I have an android app with 3 activities that has the following behaviors:
A (Home) -> B -> C
Activity A launches Activity B
Activity B launches Activity C
When user is on Activity B and they hit the Back button, it takes them Activity A
When user is on Activity C and they hit the Back button, it takes them Activity B
What I would like is when user is on Activity C, if they hit the "My Root Activity" button, it will take them to Activity A without adding a new instance of Activity A to the back stack.
So I don't want to have:
1) A
2) A-B
3) A-B-C
4) A-B-C-A
What I would like is:
1) A
2) A-B
3) A-B-C
4) A
How can I do this?
Check out the intent stack machinery!
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
EDIT:
You can add "singleTaks" or "singleInstance" to your activity in the manifest file and implement the onNewIntent() method.
"singleTaks"
The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.
"singleInstance"
Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.
Check out this link
I have each child activity extend a base activity. In the base activity, I defined an onActivityResult() which will impose a finish() if the activity at the top of the stack set a specific resultCode. So if they hit the "My Root Activity" button on Activity C, it will recursively roll up to Activity A. The Back button maintains it's functionality.
This is a rare scenario as I would have used "singleTask" but launching Activity A involves reloading it's dependencies which I don't want to couple of Activity C.