如何在屏幕旋转上保存textView文本?每次我旋转屏幕时,都会检索到一个新字符串

时间:2017-03-18 03:51:15

标签: android okhttp

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getString();
}
private void getString(){
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url(URL_MOVIE)
            .build();

    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {



            res_120 = response.body().string();
           runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                updateUI(res_120);
                            }
                        });
        }
    });

}

void updateUI(String string) {
   textView.setText(string);

}

如何在屏幕旋转中保存textView文本?每次我旋转屏幕时,都会从响应中检索到一个新字符串,并且我的textView文本会发生变化。如何阻止发生这种情况我想确保在屏幕旋转期间在textview上保留相同的字符串,直到除非我自己通过调用任何onclicklistener上的getString方法来更新它以获取textview的另一个字符串。

1 个答案:

答案 0 :(得分:0)

       String yourstring="";
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
           if (savedInstanceState != null) {
            String myString = savedInstanceState.getString("MyString"); //get it
            textView.setText(myString);
         } else {
            getString();
       }


        }
        private void getString(){
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(URL_MOVIE)
                    .build();

            client.newCall(request).enqueue(new Callback() {

                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {



                    res_120 = response.body().string();
                   runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        updateUI(res_120);
                                    }
                                });
                }
            });

        }

        void updateUI(String string) {
           textView.setText(string);
           yourstring=string;

        }
        @Override
        public void onSaveInstanceState(Bundle savedInstanceState) {

          // Save UI state changes to the savedInstanceState.   
          // This bundle will be passed to onCreate if the process is  
          // killed and restarted.


          savedInstanceState.putString("MyString", yourstring);  // save your instance

          // etc.  

          super.onSaveInstanceState(savedInstanceState);  
        }  

        //onRestoreInstanceState  

        @Override  
        public void onRestoreInstanceState(Bundle savedInstanceState) {  

          super.onRestoreInstanceState(savedInstanceState);  

          // Restore UI state from the savedInstanceState.  
          // This bundle has also been passed to onCreate.  


          String myString = savedInstanceState.getString("MyString"); //get it
          textView.setText(myString);  
        }

您可以保存instance