DSlomer64
DSlomer64

Reputation: 4283

How can I dismiss Android Settings menu programmatically?

I've extended Deitel's "Flag Quiz" (Android for Programmers...App Driven...) to (among other things) allow user to display a small flag for comparison with the given flag. The user taps SHOW FLAG and then gets a list of countries, taps one, and it's returned and shown (as ... shown below).

enter image description here

The only problem I'm having is that after tapping the desired country, the Settings preferences screen shows (see below). I guess this is because SHOW FLAG is part of it, but I want the next thing the user sees after selecting country from list is to be the screen above. There is no need to see the screen below, so there's definitely no reason to have to press the back button--how to avoid?

enter image description here


FYI, here is a trace and relevant code snippets (I can think of no reason to show any xml):

MainActivity.java:

07-06 17:25:48.305  32269-32269/com.dslomer64.flagquiz W/opOptionsItemSelected? item is <Show flag> -----------------------
07-06 17:25:48.350  32269-32269/com.dslomer64.flagquiz W/Here's where? to get flag
07-06 17:25:48.688  32269-32269/com.dslomer64.flagquiz W/onOptionsItemSelected? Show flag showFlag is true
`FlagActivity.java`:
07-06 17:25:48.768  32269-32269/com.dslomer64.flagquiz W/FlagAct? onCreate
07-06 17:25:51.707  32269-32269/com.dslomer64.flagquiz W/returnToA from FlagAct? Anguilla!!!
`SettingsActivity`:
07-06 17:25:51.751  32269-32269/com.dslomer64.flagquiz W/onCreate? SettingsActivity
(not me)
07-06 17:25:51.783  32269-32269/com.dslomer64.flagquiz W/Resources? Converting to string: TypedValue{t=0x10/d=0x3 a=-1}
07-06 17:25:51.786  32269-32269/com.dslomer64.flagquiz W/Resources? Converting to string: TypedValue{t=0x10/d=0x7d0 a=-1}
`MainActivity.java`:
07-06 17:25:55.554  32269-32269/com.dslomer64.flagquiz W/entering? onActivityResult in MainActivity
07-06 17:25:55.569  32269-32269/com.dslomer64.flagquiz W/onActResult? in Main Act show one flag Anguilla
`QuizFragment.java`:
07-06 17:25:55.591  32269-32269/com.dslomer64.flagquiz W/in showAFlag? country: Anguilla

MainActivity.java, which is where Preferences are kept and options are selected and result of calling FlagActivity (onActivityResult) is processed:

   @Override public boolean onOptionsItemSelected(MenuItem item){
      Log.w("opOptionsItemSelected", "item is <" + item.getTitle() + "> -----------------------");
      Intent preferencesIntent = new Intent(this, SettingsActivity.class);
      startActivity(preferencesIntent);
      Button getData =  (Button)findViewById(R.id.button2);
      Intent intent = new Intent(
                                  MainActivity.this,
                                  FlagActivity.class
                                 );
      startActivityForResult(intent, 0);

      Log.w("onOptionsItemSelected", "" + item.toString() + " showFlag is " + showFlag);
      return super.onOptionsItemSelected(item); // to SettingsActivity.onCreate
   }

     @Override protected void onActivityResult(int requestCode, int resultCode, Intent data ) {
        Log.w("entering","onActivityResult in MainActivity");
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == Activity.RESULT_OK){
            String enteredData = data.getStringExtra("Data");
            Toast.makeText(getApplicationContext(), "Returned " + enteredData, Toast.LENGTH_SHORT).show();;
            country = enteredData;
            Log.w("onActResult "," in Main Act show one flag " + country);
            quizFragment.showAFlag(country); 
        // ABOVE IS WHERE FLAG IS SHOWN ^^^
        }
    }

FlagActivity.java, which is where the listview is processed:

    @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flag);
    Log.w("FlagAct", " onCreate");
    flagAdapter = new ArrayAdapter<String>(this, R.layout.list_item, flagArray);
    setListAdapter(flagAdapter);
    getListView().setOnItemClickListener(itemClickListener);
}

    AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
    @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        String country = ((TextView) view).getText().toString();
        Intent result = new Intent();
        result.putExtra("Data", country);
        setResult(Activity.RESULT_OK, result);
        Toast.makeText(getApplicationContext(), country + "!!!", Toast.LENGTH_SHORT).show();
        Log.w("returnToA from FlagAct", country + "!!!");
        finish();
    }
}; // end itemClickListener

.
SettingsActivity.java:

public class SettingsActivity extends Activity // hosts SettingsFragment in portrait mode
{
    @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.w("onCreate", "SettingsActivity");
    setContentView(R.layout.activity_settings);
 }

Upvotes: 2

Views: 650

Answers (2)

DSlomer64
DSlomer64

Reputation: 4283

I fixed my problem by modifying onOptionsItemSelected, but I did not do so in terms of how I asked the Question.

  showFlag = (item.toString().equals("Show flag"));

  if(showFlag)                     // If the Settings buttonpress requested a flag,
  {                                // start the activity for it
      Intent intent = new Intent(  
              MainActivity.this,
              FlagActivity.class
              );
      startActivityForResult(intent, 0);
   }                               // but don't start a preferences activity.

   else                            // But if the Settings buttonpress was NOT 'Show flag',
   {                               // start a preferences activity.

      Intent preferencesIntent = new Intent(this, SettingsActivity.class);
      startActivity(preferencesIntent);
   };

I still don't know (and don't need to) how to "dismiss" a Settings screen.

But it would be good to know how to do so, if it's possible.

Upvotes: 0

Pratyush Yadav
Pratyush Yadav

Reputation: 271

Set an Preference.OnPreferenceChangeListener using the setOnPreferenceChangeListener() method.

Then, when the country is changed, call the finish() method to exit your activity

Upvotes: 1

Related Questions