Android - onBackPressed to turn off app - android

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.

Related

How to manage the back stack and navigate properly

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.

Top-level Android Activities

I'm trying to create a basic app using the AppCompat Drawer, and multiple top-level activities (Not fragments) - I can't quite work out how to manage the backstack - I've tried about a hundred different approaches - but they all require some sort of weird hack to either clear the backstack - or finish the existing activity.
I have 3 activities - A, B & C. A & B are top-level, C is a child of B
I want:
To start Activity A when the app starts
To exit the app when I press the back button from A
To start Activity B from the drawer
To exit the app when I press the back button from B
To start Activity C from Activity B
To go back to Activity B when I press the back button from C
When I start B from B or A from A or B from C by selecting drawer buttons - I should get the top-level Activity back in it's vanilla state.
I have:
protected void startActivity(Class activity) {
final Intent intent = new Intent(this, activity);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
overridePendingTransition(0, 0);
}
Basically I pass in either ActivityA.class or ActivityB.class - with this approach - pressing back from B takes me to A
Using HO_HISTORY, looks ok - but pressing back from C exits the app
Using REORDER_TO_FRONT doesn't seem to do anything??
Using finish() after startActivity works perfectly - unless you choose A or B twice (in which case you exit the app)
Using FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK works perfectly in every manner - except for the super nasty screen flashing as tasks are re-created. And the performance hit...
Help??
Can't you just call finish() right after your starActivity call? (you'd have to remove the SINGLE_TOP flag too -- or you'll run into the behaviour you mentioned when going from B -> B)

Removing an Activity from History Stack [duplicate]

This question already has answers here:
Remove Activities from Stack History in Android
(5 answers)
Closed 9 years ago.
Let's Suppose that I have the Sequence of the following Activities :
A -> B -> C -> D -> E
when I be in E coming from D I want to do some Actions then Go back to D without Keeping the E in the Stack.. So if I have back to D and press Back Button I will return to C not E
how to do that ???
If you add this in your android manifest for activity E it will not appear in the History stack.
android:noHistory="true"
Instead of creating a new intent to go back you should call this.finish(); on the activities that you no longer want, this will be "called" when the user pressed the back button as well, also, if you are creating a new activity and you wish for the current activity to be skipped when pressing back you can still call this.finish(); and then you can call this.startActivity(new Intent(this, ActivityName.class));
I'm not sure if I completely understand this
if I have back to D and press Back Button I will return to C not E
but if I do all you have to do is call finish() when you are done in E then you will return to D, then to C when you press back in D. If this is not what you are talking about then please clarify your question. But, when you finish() and Activity then it is cleared off of the stack so you would never return to E until you start it again. You never would from D anyway by pressing the back button
Google I/O Navigation
In its most basic form, Activities are put on the stack on top of each other in the way they come in. If you leave one, by calling finish() or pressing back, then it is removed and you are taken to the one placed before it (where you came from). There is much more to that but that is the very basic of what happens.
I think this is how you did your activity stack
A -> B -> C -> D -> E -> D (launched by intent from E).
Now you want to go from the second D to C. To do that, in E, call finish() after you start D. This should remove E from the stack.
If I understood your problem correctly;
You goes to Activity-E in a sequence of A, B, C, D, E.
Now you will come at the Activity-D when you will press the back button.
And here is the problem: When you press again back button, it goes again to Activity-E but you want to go on Activity-C here.
So you can use it in Activity E
#Override
public void onBackPressed(){
finish() ; // ActivityE.this.finish()
super.onBackPressed() ;
}

Android activity flow & navigation up/back

I'm working on an Android application that has 4 activities :
A -> B -> C -> D
When I'm going from A to B, from B to C, or from C to D, I put some extras in the Intent.
I guess navigation for something like that is quite simple: there's no need to redefine the back button, and for the "up" action bar button, a simple "finish()" would be to correct way to do it (?)
Now, the problem is that from A, I can also go directly to D. Still no need to redefine the back button, it will go back to A, and that's what I want, but for the "up" button, it must go to C, and from C, up will lead to B, ...
What is the correct way to do that?
Thanks for your answers
To control the flow between the various activities explicitly, I call finish() in each activity when I respond to user input by starting a new activity:
startActivity(intentForNewActivity);
finish();
That leads to that instance of the orignal Activity being destroyed. In each activity I create an Intent to start up the activity I want to go back to. Then write:
#Override public void onBackPressed() {
startActivity(intentWhereIWantToGoNext);
finish();
}
I originally wrote here that I called finish() in onStop(), which does work while the app remains running, but does not give the desired result if the app is stopped for any reason. Sorry for the confusion, and thanks to PravinCG, who, while his comments were not entirely on the right track, at least made me think more carefully.
One of the way is to actually have the same stack but use extras to perform the toggle.
For Instance: When you want to go from A -> D
go from A->B->C->D and use intents to handle whether you want to simply bypass activity or display it. Same is the case in reverse order user resultIntent for that.
Place the following code before the last bracket.
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}

Android - login and relogin

in my activity I have login page (L), which leads to hierarchy of activites (L -> A -> B -> C). When user log in, and he goes up to activity C, he minimizes his app and after some while, system will do a force close on this app.
Now, when he start this app again a he log-in, he should have see, where he ware last time an application was running with all opened activities on stack (if he was in C, one back button leads to B, then to A, then to L). How to achive such a behavior in Android? I am now using sharedpreference, which is hodling string of visited activities, then some flag, which tells me whether an app was finished with System close or user close and them I am persisting each activity with its own sharedpreference. If system kills my app, after login I open series of past opened activities in For cycle, but they are on the stack only. They are opened (= onCreate method is run) only when I use back button to see them.
Do you see any cleaner approach?
Thanks
When I want to maintain hierarchy after a force close I bypass the activity stack by replacing each activity with the one that it calls and then override the back event to do the same in reverse.
In ActivityL.java, where you want to go to ActivityA:
Intent intent = new Intent(getApplicationContext(), ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
You now just have one activity on the stack - ActivityA. To have the back button behave properly and return you to ActivityL, add this to ActivityA:
#Override
public void onBackPressed () {
Intent intent = new Intent(getApplicationContext(), ActivityL.class)
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
You'll still need to store enough state information to get you back to the activity where the user was before the force close. In onCreate for ActivityL, check that state info to determine which view you want the user to be on and use code similar to the first block above to go directly there.
This seems a bit cleaner to me than rebuilding the entire activity stack on startup. This does however become more complex if your activities don't follow a strict hierarchy. (i.e. sometimes activity A starts activity B and sometimes it starts activity C).

Categories

Resources