tags:

views:

50

answers:

1

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, R.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

+1  A: 

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

darbat
darbat is correct!Since most programs have more than 1 activity, it's a good idea to have your menu set as a shared menu in a seperate class. That way, you can change the settings from any activity that you allow. Stackoverflow has a good example:- http://stackoverflow.com/questions/2006457/android-how-to-have-a-shared-menu-in-each-list-activity-without-re-writing-the
andy_spoo
Thanks for the information, Unfortunately this didn't seem to do it. I had tried making a new class activity and calling it, but when testing I get and error saying the program unexpectedly quit, when pressing the settings menu item.
Alphaomega16
Nevermind...Didn't add the activity to the AndroidManifest.xml. This was the simple problem I was looking for. I had tried a new activity before but was unsuccessful. Now it works.
Alphaomega16