首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不在onRestoreInstanceState上运行函数

不在onRestoreInstanceState上运行函数
EN

Stack Overflow用户
提问于 2012-05-26 04:21:35
回答 2查看 441关注 0票数 1

我正在开发一个应用程序,当你进入一个屏幕时,你可以从onCreate中创建的对话框中选择你的位置。一旦选择了位置,它就会将其写入预先设定的TextView中。我遇到的一个问题是,当屏幕方向改变时,它会重新创建对话框,并且我试图让它不触发对话框功能。

下面是我在类文件中所拥有的基础知识。

代码语言:javascript
复制
  @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();
}

我布局中的TextView

代码语言:javascript
复制
        <TextView
            android:id="@+id/input_location"
            android:layout_width="wrap_content"
            android:layout_below="@+id/text_location"
            android:layout_height="wrap_content"
            android:text="" />

到目前为止,触发setLocation()已经尝试了几个场景来检查字符串长度,无论是否为空。当屏幕改变时,它会显示最初选择的位置,但仍然会触发对话框。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-26 04:34:59

您总是调用方法setLocation,因为每次调用ActivityonCreate方法时,form_location.getText().equals("")都将为true(因为重新创建了TextView (并且最有可能的情况是您没有在布局文件中对其设置文本)。

要避免这种情况,请使用onCreate方法的savedInstanceState

代码语言:javascript
复制
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"));
}
票数 2
EN

Stack Overflow用户

发布于 2012-05-26 04:34:11

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10760904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档