0

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?

Lucifer
  • 29,392
  • 25
  • 90
  • 143
user
  • 621
  • 15
  • 46
  • if for main activity you mean the activity currently displayed, you have only to call Activity.finish() after every startActivity/startActivityForResult – Blackbelt May 31 '12 at 15:12
  • http://stackoverflow.com/q/2033914/1289716 – MAC May 31 '12 at 15:12
  • i meant by main activity the base activity " the first lunched activity after the splash screen " , instead of making an exit button to exit the application i want to do that on the back button – user May 31 '12 at 15:16
  • Doesn't [Android do this automatically?](http://developer.android.com/design/media/navigation_up_vs_back_gmail.png) – gobernador May 31 '12 at 15:24

4 Answers4

1

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.

Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90
  • "But just don't" why ? does that affect the activity information? – user May 31 '12 at 15:21
  • He is talking about what user expect.. Probably in this case user will not know how u wrote a code and maybe he will not even notice anything unusual (in case u already came to `Tracker` from `Main`), but starting activity from onBackPressed is not common, so other developers or you in few months will have issues to go through the code and app flow.. :S btw.to achieve desired UI behavior or effect in your application standard rules and user expectations should be followed but sometimes we need to override some methods.. – Ewoks Jun 01 '12 at 07:36
0

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.

STT LCU
  • 4,348
  • 4
  • 29
  • 47
0

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..

Ewoks
  • 12,285
  • 8
  • 58
  • 67
  • thank you verrrry much , its so useful , i wonder that which is the best way of the following to exit the application : add exit menu item to exit from the application or let the application exit normally while clicking the back button ? can you help me in this – user May 31 '12 at 15:50
  • You can override onBackPressed as you tried with a slight change of excluding `startActivity(new Intent("com.MyDiet.Main"));` line.. Let's say: `public void onBackPressed() { Tracker.this.finish(); }` other option, to add exit from menu will have same `finish()` call. So both approaches are with same results, and not so unusual practice. However u might be interested in reading @RetoMeier's post about back button & exiting app. http://goo.gl/E4iR He is one of leading google android engineer and he recommends this things to be handled by Android OS. Some people agree, some not.. – Ewoks Jun 01 '12 at 07:30
0

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();
}
Vuki Limani
  • 100
  • 1
  • 5