Android Activity back manager - android

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.

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.

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

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

How to make launch activity always as home activity

I have 3 activities in my app. A is the launcher activity, when I press on a button Activity B will be started, then I have a button to start Activity A or I can press back button and I can go to A. On back press previous values retained in and on button click new values will be set on A.
When I click a button in B , A will be started, I am using REORDER TO FRONT flag and singletop as launch mode. New activity is not getting created . When I press back button on A it will transit to B and again I press back button app exits. I want to have activity A on back press on B. I cannot do anything in onBackpressed() in B as B is used in several scenarios apart from the above mentioned scenario. How to manage it.
I have nor clearly understood what you are trying to do, but you could try the up-navigation pattern (http://developer.android.com/design/patterns/navigation.html#up-vs-back).
You would have to declare in your manifest that activity A is the parent activity of B and then use NavUtils.navigateUpFromSameTask(this); in your button OnClickListener.
(NavUtils is in the support.v4 package).

fire onPause on device back button but not on button click

I am opening new activity on button click of one activity. New Activity contains image button named go back. Now I want that when anyone click on back button of mobile device, it fires onPause or say close the application but when anyone clicks on go back image button, application does not fire onPause and it goes on last activity.
How to do it ?
If you have called finish() in your first activity while coming to this second activity and if you want to go to first activity again on click event of "go back" button in second activity, then you need to call an Intent to go from second activity to first activity.
But if you do not call finish() in your first activity while coming to this second activity and you want to go to first activity again on click event of "go back" button in second activity then simply call finish() on click event of "go back" button in second activity.
I saw your code from your other question, so you need to remove the finish() from onPause and add finish() to your "go back" button listener.
Do not call finish() if you want to keep it on the stack (in order to go back).
Example:
Activity A starts Activity B, which starts Activity C. (no finish() called yet)
Then when you go back from C, call finish() and you will
see Activity B.
Then when you go back from B, call finish() and you will see Activity A.
This is how you can pass values between different activities.
Intent i = new Intent(A.this, B.class);
i.putExtra("numbers", array);
startActivity(i);

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