0

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.

@Override
public void onBackPressed() {
    finish();
    return;
}

Have I missed something or what?

Thanks in advance.

EDIT

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{       
    // Handle item selection
    switch (item.getItemId())
    {
    case R.id.menuItem1:
        setContentView(R.layout.about);
        return true;
    case R.id.menuItem2:
        System.exit(0);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

EDIT 2: About.java

package weather.right;

import weather.right.now.R;
import android.os.Bundle;

public interface About {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
    }

}
Airikr
  • 6,258
  • 15
  • 59
  • 110
  • 2
    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 Apr 03 '12 at 23:59
  • I don't know, but you can see my edit to see how I make the "window switching" – Airikr Apr 04 '12 at 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 Apr 04 '12 at 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? – Airikr Apr 04 '12 at 00:09
  • I've just added a rather detailed explanation below. Hopefully that'll help you. – Michell Bak Apr 04 '12 at 00:20

1 Answers1

1

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);
startActivity(i);

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.

Michell Bak
  • 13,182
  • 11
  • 64
  • 121
  • Thanks! I'm getting `The constructor Intent(new View.OnClickListener(){}, Class) is undefined` though when I have changed `TheNextActivity` to `About` and added About.java in com.weather.right.now > src > weather.right. I don't know why but do you know why? – Airikr Apr 04 '12 at 00:23
  • It should be `Intent(NameOfTheCurrentActivity.this, NameOfTheSecondActivity.class)`. – Michell Bak Apr 04 '12 at 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 About.java file. See my edit in the first post for more information. Why am I getting this error? – Airikr Apr 04 '12 at 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 Apr 04 '12 at 00:31
  • Thanks but I can't open the application at all now in Android Virtual Device. Do you want to see the LogCat? – Airikr Apr 04 '12 at 00:34
  • Very true. Shall we talk at the chat or in a new topic? – Airikr Apr 04 '12 at 00:39
  • Start a new topic regarding the Logcat output. Everything I've told you so far will work :-) – Michell Bak Apr 04 '12 at 00:42
  • Ok :) Then I'll start a new topic for this. Many thanks for your help! – Airikr Apr 04 '12 at 00:43