I have 3 activities in app let say A, B and C.
From A I can Navigate to B or C . So the possible workflow has two ways:
A > > B >>C (Takes input and then starts calculation upon selected formula earlier in B)
A >> C>> B and after selection of Formula it getsback to C
Problem
now in Case 1. If the user is navigated the shown path , A>B>C I want user to navigate back in the same way I mean on backpress of B he should go to C and then to A :
but in 2nd case i want user should go to A from C , I mean opening of B in C should not navigate to B when user pressback . in other words if the user has navigated using my 2nd case he should come to A not to C .
how Can i handle this in this scenario .
Basically dont override backpress in your activity, let the android handle back activity for you.
In First Usecase : when you navigate from MainActivity-->ActivityFormula-->ActivityMath. Here when you press back the sequence would be extractly reverse
In Second Usecase : When you navigate from MainActivity-->ActivityMath , do not keep ActivityFormula in stack hence the back press wont go to ActivityFormula.
#case 1, if you haven't closed A and B yourself, it will work as you want. Don't override anything.
#case 2, you can simply finish() C as soon as you start B. Write following code in your ActivityC:
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
finish();
First Usecase :Intent from ActivityA-->ActivityB-->ActivityC. Here when you press back the sequence would be extractly reverse
Second Usecase : When nevigate ActivityA-->ActivityC , and use finish(); when navigate 'ActivityC'-->'ActivityB'
and in ActivityB Intent to ActivityA by using
#Override
public void onBackPressed() {
Intent i = new Intent(ActivityC.this , ActivityB.class);
startActivity(i);
finish();
}
Let me know.. If still not getting solution
This is standard Android behaviour. Pressing the BACK button (or calling finish()) will take the user back to the previous Activity in the stack. You don't have to do anything special to get this behaviour. It works like this by default.
Related
hope you are having a great day!
So today I've got a rather simple question. In my main activity when I press the back button I want the application I've developed to turn off no matter what it did right before. And delete the stacks so it opens up fresh the next time.
The activities work like this:
A (Main Menu)
There are 4 buttons here, leading to 4 different activities B (About), C (Change Theme), D (Calculate BMI with US Measurements), E (Calculate BMI with EU Measurements)
D and E both lead to F (Results).
When I am in F and press back I want to get back to the previous (D or E) - which is fixed and it keeps the last typed in information for convenience.
On the F activity there's a button to go straight back to the menu. And if I press this button and then the standard android back button I will get from A to D/E and then to A again before closing out the application.
When I am in B, C, D, E I want to go back to A.
When I am in A I want to turn off the application.
The settings for each one of them is:
A - onBackPressed() - Here I want to put to turn off the application.
onClickA/B/C/D()
{
Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent);
finish();
}
B - android:noHistory(true)
onBackPressed()
{
Start New Intent A.
}
C - android:noHistory(true)
onBackPressed()
{
Start New Intent A (so the changes take effect on the Main Menu even if you press back).
}
D - onBackPressed()
{
Start New Intent A
finish();
}
E - onBackPressed()
{
Start New Intent A
finish();
}
F - Nothing specific.
You can use System.exit(0) method onBackPress()
Well first of all you might want to switch to fragments because what you are doing here doesn't seem so complext that an activity would be needed here.
If you switched to fragments it's easy to add those fragments to the backstack that you want to come back from.
That way you have to overwrite the onBackPressed() only once.
I have activity A and activity B. When app opens activity A opens.
I go to A to B. Then how do you "go back to A from B", without destroying B so that when you go from A to B the process is super fast? (Also how would you have to open old B and not create a new B?)
Also one more question, when you "go back to A from B", is there a way you prevent B to show up when you press back button once you are back in A?
When creating B, do all the hard work in onCreate, so later when resuming it, it starts up faster. B is destroyed only when it's not used and android needs to free some memory. Since it's in the background, switching to it will be pretty fast. To speed it up you can switch without an animation:
Intent intent = new Intent(this, B.class);
startActivity(intent);
overridePendingTransition(0, 0);
To prevent multiple B activities to be created, in your manifest's B activity tags set:
android:launchMode="singleTask"
After you go back from B to A, going back again, will quit the app. That is unless you go to A explicitly. Then you will want to override onBackPressed() in activity A, that always quits the app instead of going back to B.
Think you need to read this: http://developer.android.com/training/implementing-navigation/ancestral.html
If you want to always exit your app when pressing back in A, then you can override 'onBackPressed'.
Regarding how you would keep A 'alive' so to speak, you could set A to be 'singleInstance' or 'singleTask' in your manifest Android singleTask or singleInstance launch mode?
Here is my case
assuming that I have those activities
A -> B -> C -> D
I want to come back to A when the user click on the back button when he is in D
but also I want to come back to B when the user click on the back button of C
I thought to call finish when I go to C from B, but in this case I can't return to B if I click on back of C
how can I fix this issue without calling onBackPressed on two activities?
thanks
When you start each activity, you will need to use
startActivityForResult()
And then have a system of flags saying who is being stopped which are passed back. So, activity D:
timeToEnd() {
setResult(RESULT_D_CLOSING);
finish();
}
And then in A, C, D
onActivityResult(int requestCode, int resultCode, Intent data) {
switch(resultCode)
case RESULT_D_CLOSING:
// Close on upwards
setResult(RESULT_D_CLOSING);
finish();
case ....
// You get the idea
}
}
Have you tried using FLAG_ACTIVITY_CLEAR_TOP flag in your intent (used to start "A" from "D")?
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
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.
You have 2 options here. You can either start the activities with startActivityForResult and then onBackPressed of each activity pass a flag back that tells the activity to either exit of stay open.
An other option would be to implement A,B,C,D as fragments in the same activity put all the fragments on the backstack as you open them and then onBackPress you can programmatically loop through the backstack to see what you want to do.
As per the image shown above, I have some queries. It is requested to read each step in order :-
Each block is an Android Activity
Arrow represents the Stack Direction - the order in which activities are opened(A is started when the app was first launched)
Here when the User reaches ACTIVITY F and want to open activity Z (We are using Flag_Activity_clear_top) for the same.
After that from ACtivity Z when the user wants to open the Activity D.
****Our Requirement at this step is - When the Activity D is opened and the user do presses the back button - I WANT THAT USER SHOULD BE REDIRECTED BACK TO THE ACTIVITY C, AFTER THAT ACTIVITY B and so on..** **
Currently when we press back from the activity D(after coming from Z), then we are being redirected to the Activity Z.
CLEAR_TOP isn't good, because if you open an activity that way, it will remove the whole stack and that doesn't sound like what you want.
Try this:
When starting activity E (from D), F (from E) and Z (from F), do it with the flag "FLAG_ACTIVITY_NO_HISTORY". This flag will prevent the new activity to appear in the back stack.
Keep in mind that any activity you open this way will not be registered in the back stack. So, if you hit back while (for example) you're in F, it will return to D.
Hope this helps!
->Incase anyone is facing the same issue. Try sending the intent along with the flags 'FLAG_ACTIVITY_CLEAR_TOP' and 'FLAG_ACTIVITY_SINGLE_TOP'.
->Example mentioned in the docs: link
Consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receives the sentIntent, resulting in the stack now being: A, B.
I have two Activities, A and B. Here is a normal scenario: A is running, then sends an intent to B. A is paused, and B displays. When the user presses the back button from B, B us destroyed and the user is returned to A.
However, there is a case where B needs to re-create itself. To do this, I call finish() and then startActivity() on B and that works fine. But then, when I click the back button, it shows B again, and so I need to click the back button once more to get back to A.
How can I re-start B, but still be able to press the back button only once to return to A?
The following will dispose of the current activity while launching the next intent:
Intent launchNext = new Intent(getApplicationContext(), NextActivity.class);
launchNext.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(launchNext);
To override the back button, use the following:
#Override
public void onBackPressed() {
super.onBackPressed();
this.finish(); // or do something else
}
This can be solved by taking a closer look at your intent flags. Check out http://developer.android.com/reference/android/content/Intent.html and they give more information about what lifecycle you are shooting for.
Also, don't forget that you can override the back button functionality.
This may be helpful in case you want to manage your life cycle more closely.
For example, you can also make sure to go back to A if back from B. And close your app if back on A.