簡體   English   中英

不要在onRestoreInstanceState上運行函數

[英]Don't run function on onRestoreInstanceState

我正在開發一個應用程序,當您轉到屏幕時,您可以從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"));
}

您可以在活動標記的清單文件中設置configchange的屬性。 如果你設置。 除了您的活動之外,標志方向不會在每次方向更改時被銷毀。 因此,只會召喚一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM