user
user

Reputation: 631

how to make the appliction exit when pressing back button in the main activity?

In my application when i click the back button it passes through all the activities that i open them previously , i used the public void onBackPressed() method to make the back button back to the activity that i want as follow

public void onBackPressed()
  {
    startActivity(new Intent("com.MyDiet.Main"));
    Tracker.this.finish();
  }

is that true and safe way to code the back button ? how i can prevent the application from passing through all the previous opened activities when the back button is pressed ? and how i can make the application exit when i click the back button in the main activity?

Upvotes: 0

Views: 885

Answers (4)

Vuki Limani
Vuki Limani

Reputation: 100

You can use a lot of tricks to exit your complet application for my part i use this start a intent and then from the androidmanifest i choose the category to home and than close the current activity such would be your mainactivity !

public void onBackPressed() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    finish();
}

Upvotes: 0

Ewoks
Ewoks

Reputation: 12445

Generally speaking it's not recommended to make things work like user doesn't expect and that is considered as very bad practice. That's why it is not good to start activity from overriden onBackPressed().

When user press back by default activity will finish and one from back stack will be displayed. You as developer will (by writing code) decide which one is that. You can do it this way but later (or for somebody else) it will be a bit messy and difficult to find this unusual place for code which is starting other activity.

So..It would be useful to read about activity lifecycle and back stack to get impression how it works and understand terminology better.

If you want one of your activity not to stay on back stack you can add in manifest file file android:noHistory="true" for that activity.

Same thing you can achieve from code by adding appropriate flag to intent when you start activity: Intent.FLAG_ACTIVITY_NO_HISTORY

When user "go away" from ActivityOne (started using intent or defined in manifest like described), to new ActivityTwo and then press back, it will not go to ActivityOne because it will not be on back stack. Using this you can precisely control navigation through your activities.

More flags are available for use, but I guess this is what you wanted to achieve.

Hope you will find my answer useful. Cheers..

Upvotes: 0

STT LCU
STT LCU

Reputation: 4330

This approach isn't good, because you're polluting the activity stack of your application

Example:

current stack: MainAct -> Act2 -> Act3 (we're in activity 3)

With your code above, when you press back, the stack now looks as follows:

MainAct -> Act2 -> MainAct

Because you ended Act3 and launched a NEW main activity, which may be not what you wanted.

To achieve what you want (Get back to main when the current activity is over) you need to work on the intermediate activities: In the example above, when from Act2 you call startActivity("Act3"), you should call "this.finish()". Therefore you don't have to override "onBackPressed()" of activity 3: simply the default value will terminate Act3 and the next activity in the stack will be MainAct

MainAct -> A2 (A2 launches A3 and then calls this.finish())

MainAct -> A3 (user now press back)

MainAct (the mainactivity is now on top)

To summarize, you don't have to override onBackPressed, you just have to correctly manage the lifecycle of the activity between the main one and the current one.

Upvotes: 0

Charlie-Blake
Charlie-Blake

Reputation: 11050

In your application, and in ALL android applications, unless it's critical not to pass through unneeded steps (such as login if you're already logged in), it's VERY important not to override Android standard behaviour. Users normally complain about Android apps not having a common behaviour or style guideline.

Anyway, yeah, you can just override onBackPressed on all your activities and do whatever you want. But just don't.

Upvotes: 1

Related Questions