简体   繁体   English

不要在onRestoreInstanceState上运行函数

[英]Don't run function on onRestoreInstanceState

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 . 我正在开发一个应用程序,当您转到屏幕时,您可以从onCreate创建的对话框中选择您的位置。 Once the location is selected it writes it into a predined TextView . 选择位置后,它会将其写入预定义的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

        <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. 至于触发setLocation()已经尝试了几种方案来检查字符串长度,无论是否为null。 When the screen changes it shows the original chosen location, but still fires the dialog. 当屏幕更改时,它会显示原始选择的位置,但仍然会触发对话框。

You always call the method setLocation because each time the onCreate method of the Activity is called form_location.getText().equals("") will be true (because the TextView is recreated(and most likely you don't set text on it in the layout file)). 你总是调用方法setLocation因为每次调用ActivityonCreate方法时都会调用form_location.getText().equals("")将为true (因为TextView被重新创建(很可能你没有在其上设置文本)布局文件))。

To avoid this, use the savedInstanceState of the onCreate method: 要避免这种情况,请使用onCreate方法的savedInstanceState

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

You can set in the manifest file in the activity tag an attributed of configchange. 您可以在活动标记的清单文件中设置configchange的属性。 If you set. 如果你设置。 The flag orientation than your activity will not be destroyed on every orientation change. 除了您的活动之外,标志方向不会在每次方向更改时被销毁。 So onceate will only be called once. 因此,只会召唤一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM