1

I am working on an app that when you go to a screen you select your location from a dialog which is created within onCreate. Once the location is selected it writes it into a predined TextView. A problem that I am having is when the screen orientation changes it recreates the dialog and I'm trying to have it not fire the dialog function.

Here is the basics of what I have within the class file.

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.emergpolice);

    form_location = (TextView) findViewById(R.id.input_location);

    if(form_location == null || form_location.getText().equals("")){
        setLocation();
    }
}

@Override
protected void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putString("LOCATION", (String)form_location.getText());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
    super.onRestoreInstanceState(savedInstanceState);
    form_location.setText(savedInstanceState.getString("LOCATION"));
}

public void setLocation(){
    db = new DatabaseHandler(this);
    db.open();
    final CharSequence[] locOpt = {getString(R.string.dialog_items_current_location),getString(R.string.dialog_items_home),getString(R.string.dialog_items_enter_manually)};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getString(R.string.header_choose_location));
    builder.setSingleChoiceItems(locOpt, -1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item){
            if(locOpt[item].equals(getString(R.string.dialog_items_home))){
                Cursor cur = db.userInfo(); 
                String address = cur.getString(cur.getColumnIndex("address"));
                String city = cur.getString(7);
                String county = cur.getString(8);
                String state = cur.getString(9);
                String zip = cur.getString(10);
                db.close();
                form_location.setText(address + ", " + city + ", " + county + ", " + state + ", " + zip);
            }
            if(locOpt[item].equals(getString(R.string.dialog_items_current_location))){
                Toast.makeText(getApplicationContext(), locOpt[item], Toast.LENGTH_SHORT).show();
            }
            dialog.cancel();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

And the TextView in my layout is

        <TextView
            android:id="@+id/input_location"
            android:layout_width="wrap_content"
            android:layout_below="@+id/text_location"
            android:layout_height="wrap_content"
            android:text="" />

As far as firing setLocation() is have tried several scenarios to check the string length, whether null or not. When the screen changes it shows the original chosen location, but still fires the dialog.

4

2 回答 2

2

您总是调用该方法setLocation,因为每次都会调用该方法(因为重新创建了该onCreate方法(并且很可能您没有在布局文件中设置文本))。Activityform_location.getText().equals("")trueTextView

为避免这种情况,请使用savedInstanceState以下onCreate方法:

if (savedInstanceState == null){
   // if savedInstanceState is null the activity is created for the first time
   setLocation();
} else {
   // if not null then the activity is recreated so restore the TextView text
   form_location.setText(savedInstanceState.getString("LOCATION"));
}
于 2012-05-25T20:34:58.997 回答
1

您可以在活动标签的清单文件中设置 configchange 的属性。如果你设置。每次方向更改都不会破坏您的活动的标志方向。所以 onceate 只会被调用一次。

于 2012-05-25T20:34:11.483 回答