Pressing the "go back" button closes the application

Go To


I'm currently moving slowly but steady forward in the making of a Android application and Im currently learning how to create a new window and switch to it. This is going all well but I have one small problem. When I pressing the "go back" button closes the application even if I have choosed to go back when just that button is pressed.

public void onBackPressed() {

Have I missed something or what?

Thanks in advance.


public boolean onCreateOptionsMenu(Menu menu)
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(, menu);
    return true;

public boolean onOptionsItemSelected(MenuItem item)
    // Handle item selection
    switch (item.getItemId())
        return true;
        return true;
        return super.onOptionsItemSelected(item);


package weather.right;

import android.os.Bundle;

public interface About {
    public void onCreate(Bundle savedInstanceState) {

2012-04-03 23:54
by Erik
finish() will end the current activity. Are you sure that other activities are still running? In most cases it's actually better to leave Android to handle the back button, and not override the function. Can you post the code that you use to launch the current activity as well - joshhendo 2012-04-03 23:59
I don't know, but you can see my edit to see how I make the "window switching - Erik 2012-04-04 00:02
I don't understand why you're using System.exit() or setContentView() in your MenuItems. You're calling finish() when you override the Back button, so whatever Activity is currently running when you press it will close - adneal 2012-04-04 00:06
I readed an guide about how I create this "optionsmenu" and copy-pasted the code from there. According to this page (Stack Overflow), System.exit(0) closed the application for real unlike finish(); which I used before I switched to System.exit(0). What should I use - Erik 2012-04-04 00:09
I've just added a rather detailed explanation below. Hopefully that'll help you - Michell Bak 2012-04-04 00:20


You need to use Intents to "switch windows". A "window" is called an Activity in Android and you set the visual content of an Activity using the setContentView() method in the onCreate() call of the Activity.

To launch another Activity from your current Activity, simply create an Intent with a few parameters and call startActivity() with that Intent. Here's an example:

Intent i = new Intent(this, TheNextActivity.class);

Don't forget to include your second Activity in the Android manifest file. All Activities in your application must be included in that file.

Other things to note is that you don't really use System.exit() in Android. Just call finish(). It's advised to let Android manage applications and its resources rather than doing it yourself, but if you want to make sure that your application really is shut down, feel free to use System.exit() anyway. There's also no need for overriding onBackPressed() if you're only calling finish(). That's standard behaviour in Android when you hit the back button.

Also, you don't call setContentView() more than once per Activity. You start a new Activity when you need to change the visuals (or use one of the specialized Widgets to switch between layouts.

This also explains why you're experiencing your "problem". You may have changed the layout of the Activity using setContentView(), but there's still only one Activity running - when you call finish(), that Activity gets closed. If you had started a second Activity with a different layout, like you're supposed to do, Android would have closed that second Activity and would have returned you to the first.

2012-04-04 00:07
by Michell Bak
Thanks! I'm getting The constructor Intent(new View.OnClickListener(){}, Class<About>) is undefined though when I have changed TheNextActivity to About and added in > src > weather.right. I don't know why but do you know why - Erik 2012-04-04 00:23
It should be Intent(NameOfTheCurrentActivity.this, NameOfTheSecondActivity.class) - Michell Bak 2012-04-04 00:24
Many thanks! I'm getting an new error though - Abstract methods do not specify a body? on public void onCreate(Bundle savedInstanceState) { in the file. See my edit in the first post for more information. Why am I getting this error - Erik 2012-04-04 00:30
It should be a class, not an interface. And the class needs to extend Activity. Basically just replace "public interface About" with "public class About extends Activity" - Michell Bak 2012-04-04 00:31
Thanks but I can't open the application at all now in Android Virtual Device. Do you want to see the LogCat - Erik 2012-04-04 00:34
Sure, but this is getting a bit off track from the original question.. - Michell Bak 2012-04-04 00:37
Very true. Shall we talk at the chat or in a new topic - Erik 2012-04-04 00:39
Start a new topic regarding the Logcat output. Everything I've told you so far will work :- - Michell Bak 2012-04-04 00:42
Ok :) Then I'll start a new topic for this. Many thanks for your help - Erik 2012-04-04 00:43