且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我的Andr​​oid应用程序不会退出时pressing后退按钮

更新时间:2022-11-06 17:08:31

您layerout XML文件中使用的MediaController作为布局根,与LinearLayout中更换

I am new to Android programming and I wrote an app implementing only the Activity.OnCreate method. I expected to go back to the previous view (which has to be the home screen due to the basic intent-filter I have) by pressing the BACK button but it does not. I managed to do that only by pressing the HOME button. (I have tested my app on multiple devices and it behaves the same.) Has everybody else went into the same problem? What is the solution? (the git repo of my app is here: https://github.com/falcondai/android_lab1)

The following is the Activity I failed to back out properly (the BACK key works fine for all other activities):

public class NameGetterActivity extends Activity 
{
    private static final String TAG = "NameGetterActivity";

    private EditText name_fld;
    private Button submit_btn;
    private Button test_btn;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.name_getter);

        name_fld = (EditText)this.findViewById(R.id.editText1);
        submit_btn = (Button)this.findViewById(R.id.button1);
        test_btn = (Button)this.findViewById(R.id.button2);

        submit_btn.setOnClickListener(
                new OnClickListener() {
                    public void onClick(View v) {
                        Log.d(TAG, name_fld.getText().toString());

                        Intent i = new Intent(NameGetterActivity.this, HelloWorldActivity.class);
                        i.putExtra("name", name_fld.getText().toString());
                        startActivity(i);
                    }
                }
            );

        test_btn.setOnClickListener(
                new OnClickListener() {
                    public void onClick(View v) {
                        Log.d(TAG, "sensor activity button pressed");

                        Intent i = new Intent(NameGetterActivity.this, SensorActivity.class);
                        startActivity(i);
                    }
                }
            );
    }

}

your layerout xml file use MediaController as layout root, replace it with LinearLayout