Moving forward and backward with multiple activities - android

I have 4 activities(ActivityA, ActivityB, ActivityC, ActivityD).
I Moved from ActivityA -> ActivityB -> ActivityC -> ActivityD.
Now I want to know what I do in fallowing 2 conditions.
When go to ActivityB from ActivityD by skip ActivityC
When go to ActivityA from ActivityD by skip ActivityB & ActivityC
Note: Here i could not want to call finishaffinity method to finish all activity and launch desired Activity.

Declare your activity Activity A and Activity B in manifest with the SingleTask launch mode.
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. It will also clear all the activities which are over that Activity. For eg- If you start Activity B from Activity D, then initially your stack would be A->B->C->D which will then be changed to A->B.
Check this out for the complete official documentation
Hope it helps.

Related

Android "singleTask" activity creates new instance of the Activity

I have two different application called App A and App B.
App A : ActivityA with launch mode -> "singleTask"
App B : ActivityB with launch mode -> "singleInstance"
Steps:
Open App A which will open ActivityA, ActivityStack : ActivityA
Starting App B with startActivityForResult(), ActivityStack : ActivityA > ActivityB
Open ActivityA via Intent from Activity B , ActivityStack : ActivityA > ActivityB > ActivityA
As you can see the activity stack, It creates new instance of the Activity A instead of returning to the same instance of the ActivityA by calling onNewIntent() method.
I am not sure, is this because of Activity started for result.? Please help to provide proper solution in this scenario. Thank you.
You cannot start a singleTask Activity using startActivityForResult(). If you launch an Activity and expect a result returned, then the target Activity must be launched in the same task. Due to this, Android is ignoring the singleTask launch mode when you call startActivityForResult().

How manage my activities on android stack?

In my app from activityA that contain two fragments starts activityB and from both of them can start activityC and activityD now from activityC and activityD I want to back to activityA. I do all of them with startActivity there for lots of instance of activities save in my stack and if I want to close my app should pass through them. Is any body have an idea how should I remove this problem?
ok this is simple when you go from activityB to activityC and ActivityD just after startActivity() use finish();
This will kill your ActivityB and when you want to come back from C or D to A you can do that...
or you can find more help here .
http://developer.android.com/training/basics/activity-lifecycle/starting.html
Hope it helps .... :)

Intent flags on Android

I have a widget for my application, which need to be somewhat independent from the app.
The activity workflow should be like this:
Widget -> Activity acting as receiver
Receiver -> LoginPage or Activity A (depending on login status)
LoginPage -> Activity A
Activity A onKeyDown -> Activity B
Activity B onKeyDown -> Home Screen.
I have no problem until Activity B, which sends back to Activity A when I press onKeyDown. I'm using FLAG_ACTIVITY_CLEAR_TOP flag and finishing the Activity when starting the activity B.
When I move from ActivityA to ActivityB using the CLEAR_TOP flag, I supposed that Activity stack is cleared, then in ActivityB I finish the Activity on the onKeyDown() method, assuming that the App will be closed, but it doesnt. Why?
I'm also trying to use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK in the receiver but I dont understand the mechanism pretty much. Any idea about this?
In fact the FLAG_ACTIVITY_CLEAR_TOP, start your activity B if its not started or it came back as the second activity on the BackStack. To finish Activity A, you can call finish() after starting Activity B or add no history flag, when starting A.
#JesusS: I doubt if u can finish ur activity in that fashion during a forward transition.
Consider a scenario of moving from Activity A to Activity B. Now if u want to kill Activity A and want to move to Activity B then call the startActivity(intent);
(where ur moving from activity A to B)
without any flags on the intent followed by the finish() on activity A.
As per my understanding u can use Intent.FLAG_ACTIVITY_CLEAR_TOP only during backward transition i.e when u already have that activity on the stack.
Consider the following scenario:
A --> B --> C --> D
Now if u want to move back from activity D to Activity A by clearing the activities u can go for Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP.
The result is that the Activities D, C, B(LIFO) will be removed from the stack and the activity A resumes by calling the onResume() of Activity A.

Do I need to explicitly finish an Activity in Android?

When I am starting a new activity do I need to explicitly finish the current activity or does android take care this ?
This is what I write in activity A to start activity B:
Intent intent = new Intent(this, BActivity.class);
startActivity(intent);
Should I end A by calling next line after above mentioned two lines ?
this.finish()
In General no you shouldn't.
The difference will be if you call finish in Activity A, While the user is in Activity B if they press the back button they will go back to whatever they were doing before opening your application. If you instead do not call finish in Activity A they will go back to Activity A
If you DO call finish:
Activity A -> Activity B -> [user press back] -> Homescreen (or whatever activity is on the stack below activity A)
if you DO NOT call finish:
Activity A -> Activity B -> [user press back] -> Activity A
No it is not compulsory.
finish()
finish method state that "Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult()."
reference link >> link
explicitly finish the current activity or does android take care this ?
It depends on your requirement if you wants activity A while coming back form activity B still there so you need not to call finish but if you does not want activity A when coming back form activity B then you should call finish ....

How do I collapse "child activities"?

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.

Categories

Resources