How manage my activities on android stack? - android

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 .... :)

Related

Moving forward and backward with multiple activities

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.

Android back stack

I have an application with several Activities in Android A,B,C,D. I have a question about the back stack.
A->B->C->D it is a normal sequence.
My question is : when i press the back button in Activity D, i have to go to the Activity B, not back to ActivityC. Since ActivityC have some imageView i want to save, i don't want to use noHistory to destroy the ActivityC. it is possible to do that
A1->B1->C1->D1
the back stack should be: D1->B1->A1 . C1 saved some imageView and if C1 launch again, the imageView in C1 will contain the same images. Is it possible just modify the code in ActivityC??
thank you
You should call finish() in activity C after, starting of activity D
startActivity(intent);
finish()
Handle onBackPressed() of each activity. So that after back pressing on D, it will redirect to B, then similarly to A.
Use onActivityResult() methods of an activity:-
Here is how it will work -
Use startActivityForResult() method while you are starting activity
While when you are going back
Activity D - before finishing write -
Intent i = getIntent();
setResult(RESULT_OK);
finish();
Your D is finished and c's onActivityResult() will be called - save your data and call finish() with above methodology, It will go to B's onActivityResult().

Android - How do I switch between 1st and 3rd activities?

I have a 1st and a 3rd activity. I wish to push a button on the 3rd activity and it finishes the 3rd and 2nd activity switching all the way back to the 1st activity(it's the MainActivity might I add). In other words, is there a way to finish automaticly the call stack of activities to a specific activity?
You can use method startActivityForResult() to start your activity.
You need to call method setResult() when you finish the activity.
And override method onActivityResult() to do what you want to do.
Actually, seems 2 methods can solve this.
Assuming Activities: A -> B -> C, and all alive in the same stack.
1) Use android:launchMode="singleTask" for Activity A. When C calls A, A starts and both B & C finish.
2) When starting A, adding flag Intent.FLAG_ACTIVITY_CLEAR_TOP.
if you don't have to pass any data to the main activity, i guess this is what you are looking for: how back to the main activity in android?
execute this code at the button click in your 3rd activity:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this code clears your activity stack and reopens your main activity.

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.

About android launchmode "singleTask"

I read the android developer guide and some articles in internet, I'm still confusing about the singleTask launchmode. Lets take an example:
User launch the App1, android starts a new task. Assume the App1 creates activities in follow order:
ActivityA -> ActivityB -> ActivityC
That's how task1 looks like.
Then user click the home buttom and choose to launch App2, so task1 goes in background and android start a new task: task2, user does something:
ActivityD -> ActivityE
now lets say ActivityE try to start ActivityB , and ActivityB has the launchmode singleTask.
What I understand is that task1 comes to frontend again and task2 goes to background. And task1 looks now like this:
ActivityA -> ActivityB
Which means:
The ActivityC will be removed from task1 and ActivityB becomes to the top Activity.
If user now click on "Back" button, he will come to ActivityA of task1 instead of back to ActivityE of task2
Am I right?
Thanks
You sound right.
Why don't you test it.
There is also this app that can help explain launch mode:
https://play.google.com/store/apps/details?id=com.novoda.demos.activitylaunchmode
Sources are at https://github.com/gnorsilva/Activities-LaunchMode-demo
The ActivityC will be removed from task1 and ActivityB becomes the top Activity.
Yes, you are Right...
ActivityC will be removed from i.e. the onDestroy method of the ActivityC will be called. Hence when the user launches Task 1 again, the ActivityB is shown rather than ActivityC.
Have created 2 Tasks (Projects) and uploaded the same # SendSpace. Try it out...
If you look at androids documentation it says
" A "singleTask" activity allows other activities to be part of its task. It's always at the root of its task, but other activities (necessarily "standard" and "singleTop" activities) can be launched into that task."
This means that when you click the home button all the activities above the single-task activity (which in your case is ActivityB) are removed from the stack.
In the sample, the app's I had given you earlier if you just run the project "AndroidTest" and click the home button in the logs you can see that the 2nd Activity is put on Pause, and when you launch it again from the "Recent App's" list the 2nd Activity is Destroyed.
In a scenario where the Activity's above the Single Instance activities (ActivityB) are not removed from the Back Stack, and another application request this Activity (ActivityB) it may not be shown and the intent may be dropped. But this has extremely fewer chances of happening because the user will have to press the Home button and but the current Task\App in the BackStack before he could navigate to another Task\App.
Hence the warning
The other modes — singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications.
I hope this solves your doubts.
Correct, whenever singleTask activity is launched it comes foreground clearing all activities currently present above it, if your singleTask activity is on the top, it will behave same as singleTop.
P.S - onCreate is not called 2nd time, instead onNewIntent is called.
check this link, very well explained about launchmodes.
https://medium.com/android-news/android-activity-launch-mode-e0df1aa72242

Categories

Resources