android - 不要在 onRestoreInstanceState 上运行函数

标签 android restore oncreate

我正在开发一个应用程序,当您转到屏幕时,您可以从 onCreate 中创建的对话框中选择您的位置。选择位置后,它会将其写入预定义的 TextView。我遇到的一个问题是,当屏幕方向发生变化时,它会重新创建对话框,而我试图让它不触发对话框功能。

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

  @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

        <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() 而言,已经尝试了几种情况来检查字符串长度,无论是否为 null。当屏幕发生变化时,它会显示最初选择的位置,但仍会触发对话框。

最佳答案

您总是调用方法 setLocation,因为每次调用 ActivityonCreate 方法时都会调用 form_location.getText()。 equals("") 将为 true(因为 TextView 已重新创建(很可能您没有在布局文件中设置文本)) .

为避免这种情况,请使用 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"));
}

关于android - 不要在 onRestoreInstanceState 上运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10760904/

相关文章:

java - 像网络摄像头一样使用安卓手机

java - 解绑外部服务android Paho MQTT ServiceConnectionLeaked报错

java - 如何在线程内更改 View

mysql - 如何备份 MySQL 数据库中的单个表?

android - 究竟何时调用 onSaveInstanceState() 和 onRestoreInstanceState()?

sql-server-2005 - 如何将 SQL 2005 数据库复制到另一台计算机?

Android资源未找到异常oncreate

android - 通过 ADB 安装用户证书

android - Activity 确实出现了

ANDROID Activity 在重启时强制停止