Start new task with predefined backstack (activity B ontop of activity A) - android

Is there a way to start a task with an activity B in the foreground but with an activity A on the backstack? So when I start B via intent and press the back or home button A is active.
At the moment I can start a new task via intent flag FLAG_ACTIVITY_NEW_TASK and start B but of course my backstack is empty so when I hit the back button it will return to the home screen.
Only if the app was running before (activity A already exists). Starting activity B will result in the correct backstack state A,B where B in ontop of A.

when u backpress from that condition then set one flag boolean and whenever u backpress then check that falg if true then in back button hust pass Activity intent for A :)

Related

Android back button behaviour issue...

I have been using finish() for back button presses to go back to previous activity and its working fine but it is not working as expected for this specific scenario.
Here are a list of my activities and its functions:
Activity A - Show online forum topics
Activity B - Show the comments of a forum topic
Activity C - Post a new comment
After a user post a new comment, he will be directed to activity B.
Issue:
When i click on back button on Activity B, it will go back to Activity C, because it was my previous activity.
Activity A -> Activity B -> Activity C -> Activity B -> Activity C -> Activity B -> Activity A
Expectation
I want the user to go back to activity A from activity B at all cause.
Activity A -> Activity B -> Activity C -> Activity B -> Activity A
I tried using intent to only direct Activity B to A upon back pressed, but it is reloading the data on Activity A which i do not want.
Codes I tried:
case android.R.id.home:
finish();
return true;
//this works if i am only going back and forth one activity
case android.R.id.home:
Intent i = new Intent(ActivityB.this, ActivityA.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("forumLink", forumLink);
i.putExtra("forumTitle", forumTitle);
startActivity(i);
//this refreshes Activity A data
Call finish() on Activity C before going from C to B.
You can make Activity A as parent of B ,and B as parent of C
You have to kill Activity C before going to activity B.
Since your activity c exits, thats why it is going to activity c.
So, use finish() to kill activity c. Killing the activity c will redirect you to activity b.

Android Activity back manager

In my android activity I have 3 activity A B C
But when i start A - >B - >C - >A and when i click back all activity remain A-C-B-A
I want when I press back in current activity it returns to activity A
Help
When you want an Activity to not be added to the back stack after leaving it just call:
finish();
So for example, to start Activity B from Activity A and get rid of Activity A completely:
startActivity(new Intent(ActivityA.this, ActivityB.class));
finish();
Now if you press the back button in Activity B, it will exit the app because there are no other Activities in the back stack.
For your specific situation, you can call finish on everything except Activity A, which will make Activity A the only Activity in your back stack. So the back button will always send you back to Activity A no matter what Activity you press it from.

How to go back to Home Activity from a specific activity without overriding back button

I am new to Android . Here I have for activities A,B,C,D in which A is the Home Activity.It is in stack as A->B->C->D
When I press back from B or C it should go back just as normal. But if I press back from D it should go back to A and from A the app should exit
I guess you could intercept onStop() and guessing if the activity is switching to C and launching A instead. But it would result in a hard to maintain mess and I do not recommend that.
However, if for some reason you still not want to override onBackPressed and you manage to guess that D is stopping because back was pressed (without overriding onBackPressed(), just start A activity from there with an Intent with FLAG_ACTIVITY_CLEAR_TOP (call i.setFlags(FLAG_ACTIVITY_CLEAR_TOP) )
According to the doc:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
So A will be brought back and B and C will be cleared.
when you start your new activity finish the old activity for example if you want to start C from B :
Intent I = new Intent(B.this , C.class) ;
startActivity (I);
B.this.finish();

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