launching new activity keeping launcher activity in backstack - android

There are two activities in my application Activity A ( Launcher Activity) and activity B. first i launch an application(A activity) . onBackpress(),i am moving A to backstack. I am launching other app and firing intent from there to launch activity B. Now activity A which is in background is getting launched first then B is coming top of it.
How to make activity A to be in backstack and opens only activity B on firing intent from other application?

Related

How to reorder singleTask activity without creating new one?

I have 2 activity (Activity A and Activity B)
Activity A is singleTask and Activity B has standard launch mode.
When application started with Activity A and launch mode is singleTask, now from activity A start new activity B on button click.
So Stack is Activity A, B
Now wanted to reorder Activity A which is single task from Activity B but it's finishing Activity B first and then create new Activity A.
So stack at the momemnt is only Activity A.
What I've tried from Activity B.
val intent = Intent(this, ActivityA::class.java)
intent.flags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
startActivity(intent)
Problem: Without finishing Activity B required to reorder Activity A with launch mode singleTask.
Appreciated in advance.
You cannot reorder an Activity with singleTask launch mode to the front of the stack. An Activitywith singleTask launch mode is by definition the root Activity in a task.
Why would you want to do this? If you've configured the Activity as singleTask, you have told Android that you want this Activity to be launched as the root of a task. Why would you want to reorder this Activity to the front of the task? If the Activity can exist in the task somewhere other than the root, you shouldn't define it as singleTask!

Android activity navigation issue

I have a problem with navigating through activities. I am navigating from HomeActivity to Activity A. When I move from Home to Activity A, I finish the HomeActivity and starts another activity Activity B. When I start Activity B, I am not finishing Activity A. So when I finish Activity B normally it should go to Activity A. But In my case it is not working. When I call finish() in Activity B, both Activity B and Activity A is getting finished.
Can anybody suggest a way to accomplish this?

Android start main activity from home launcher

In my app I have a main activity that is started when the app is launched from the app drawer. In the app there is another activity B that can be launched from the main activity.
If I press home while in activity B to return to home, Activity B will be started instead of the main activity if I launch my app through the app drawer again.
Is there a way to make only the main activity start even if the user pressed home in activity B without using noHistory?
Any help would be appreciated!
You just don't want to keep state in B, on the Activity B onStop method, just call finish();
I set a separate android:taskAffinity for activity B

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.

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