开发者

How to open options menu, change setting, and return - Android?

I am attempting to get my app to open a settings menu screen and return. The problem I am having is when in the settings page, upon pressing the back button, the app closes. I am VERY new to programming in general after fighting this for about 8 hours I am ready to ask for help!

Here is the code I have written

`package com.bowersoftware.connecttozcu;

import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast;

public class ConnecttoZCU extends Activity {

private Spinner mEngineSpinner;
private Spinner mUnitsSpinner;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mEngineSpinner = (Spinner) findViewById(R.id.engineSpinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.engine, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mEngineSpinner.setAdapter(adapter);

}

private void ConnectSettings() {
    setContentView(R.layout.settings);
    mUnitsSpinner = (Spinner) findViewById(R.id.unitsSpinner);
    ArrayAdapter<CharSequence> settingsadapter = ArrayAdapter.createFromResource(
            this, 开发者_StackOverflowR.array.units, android.R.layout.simple_spinner_item);
    settingsadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mUnitsSpinner.setAdapter(settingsadapter);

}

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


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.scan:
        Toast.makeText(this, "Scan", Toast.LENGTH_LONG).show();
        return true;
    case R.id.settings:
        Toast.makeText(this, "Settings", Toast.LENGTH_LONG).show();
        ConnectSettings();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

}`

I am sure it is something silly I am missing but just can't figure it out.

Thanks, Jason


ContactsSetting should be separate Activity.

At the moment you have single Activity, so when you press Back button the application is closed.

To start new activity use:

startActivity(new Intent(this, ContactsSetting.class));

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜