Intent flags on Android - 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.

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.

Go back to Activity without recreating (without invoking onCreate())

I would like to ask similar question to:
Go back to previous screen without creating new instance
However I don't want Activity A go through onCreate callback, I would like to return to previous instance in the same state. How to achieve that without calling multiple finish() ?
There's no need to finish() activities as you navigate through your application. Instead, you can maintain your Activity back-stack and still achieve your goal. Let's say you have 4 activites like so:
A --> B --> C -->D
where D is the topmost activity and A is root activity. If you are 'falling back' to activity B, then you'll need to do two things in order to avoid hitting B's onCreate method.
1.) Make B a "SingleTask" activity. You can do this in the Android Manifest. To be brief, this means that only one 'instance' of B will ever exist within this Task. If B is already running when it's called then it will simply be brought to the front. This is how you do that.
<activity
android:name=".ui.MyActivity"
android:launchMode="singleTask"/>
But you don't want to just bring B to the front. You want to 'fall back' to B so that your stack looks like
A--> B
2.) Add this flag to your intent that 'starts' B. This ensures that C and D are removed.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Now when 'D' calls new Intent to B, B will be resumed and C and D will be removed. B will not be recreated, it will only call onNewIntent.
When navigating from A to B do not finish Activity A.
When navigating from B to C finish Activity B.
When navigating from C to D finish Activity C.
So from wherever you navigate back finish current Activity and Activity A will get resume again not get created.

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.

What is the difference between Intent.FLAG_ACTIVITY_CLEAR_TOP and finish in android

What is the difference between Intent.FLAG_ACTIVITY_CLEAR_TOP and finish() in Android?
The differerence between these two are as follows:
1.finish() you can use to end the activity in which you are right now present and also it will end one activity at one time.
2.In case of FLAG_ACTIVITY_CLEAR_TOP,It will end all the activities those are on top of the current activities inside the stack.There may be more than one activity.
suppose you are starting activities one after another in the order
A-->B-->C-->D,ie activity B started from activity A,activity C started from activity B and so on.
Now calling startactivity(A) from activity D with intent flag FLAG_ACTIVITY_CLEAR_TOP finishes all activities in between (here B and C) and starts A.
calling Finish() from your activity closes current activity
finish() android uses to end the activity by calling it in program.
(Note, you can also use onDestroy()).
FLAG_ACTIVITY_CLEAR_TOP clears all the activities that are top of the current activities inside the Activity stack.

Navigate back to Home Activity

I've requirement for application to set a Buttonin every activity to go back to HomeActivitybut I should Not Reload the content for it, so I need to re-use the instance I already have of HomeActivity, how I could do that?
You should use: FLAG_ACTIVITY_REORDER_TO_FRONT
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT
If set in an Intent passed to Context.startActivity(), this flag will
cause the launched activity to be brought to the front of its task's
history stack if it is already running. For example, consider a task
consisting of four activities: A, B, C, D. If D calls startActivity()
with an Intent that resolves to the component of activity B, then B
will be brought to the front of the history stack, with this resulting
order: A, C, D, B. This flag will be ignored if
FLAG_ACTIVITY_CLEAR_TOP is also specified.
I used FLAG_ACTIVITY_CLEAR_TOP for home button in my activities. If you have your HomeActivity already in application stack, this flag causes close of all activities above your HomeActivity. It depends if you need to reorder HomeActivity to front (Back button will return you back to activity where you clicked home) or you want to close all activities that are above HomeActivity (like clicking back until I'm in HomeActivity, in my case Back button closes application from my home activity).
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

Categories

Resources