English 中文(简体)
不运行Restore Instance State 上的函数
原标题:Don t run function on onRestoreInstanceState

我正在研究一个应用程序,当您去屏幕上时, 您可以从 onCreate 中创建的对话框中选择您的位置。 一旦选择了该位置, 它就会将其写成预设的 < code> 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();
}

在我布局中的 < code> 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="" />

开火 < code> setLocation () () < /code > 已经尝试过几个方案来检查字符串长度, 不论是否空。 当屏幕更改时, 它会显示最初选择的位置, 但仍然会点燃对话框 。

最佳回答

你总是叫这个方法 setLocation ,因为每次 onCreate 方法被称作 activity /code> 方法都被称为 form_love.getText (".getText (".getText (")) 将是 tret (因为 TextView 被重新创建(而且很可能您不会在布局文件中设置文字)。

为避免这种情况,使用 saveedInstance State 方法 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"));
}
问题回答

您可以在活动表单文件中设置一个配置更改的属性。 如果您设置了, 则在每次方向变化时不会销毁您活动的旗帜方向。 因此, 一次性只会被调用一次 。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签