Pressing the “go back” button closes the application

给你一囗甜甜゛ 提交于 2019-12-25 10:01:10

问题


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.



来源:https://stackoverflow.com/questions/10003123/pressing-the-go-back-button-closes-the-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!