-->

Pressing the “go back” button closes the applicati

2019-09-13 01:24发布

问题:

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);
    }

}

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