【问题标题】:Saving textview in a fragment when rotating screen旋转屏幕时将文本视图保存在片段中
【发布时间】:2013-09-26 21:14:16
【问题描述】:

当我旋转屏幕时,我在将作为选项卡附加到操作栏中的 sherlockframent 中的 TextView 保存然后恢复文本时遇到问题。我覆盖 onPause() 和 onActivityCreated(Bundle savedInstanceState) 方法,如下面的代码:

@Override
public void onPause() {
      super.onPause();
      Bundle outState = new Bundle();
      String str = memoryText.getText().toString();
      outState.putCharSequence("app.multicalc.basiccalcfragment.save", str);

      Log.v("saving", "text saved");

    }

@Override
public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      if(savedInstanceState!=null){
          CharSequence str = savedInstanceState.getCharSequence("app.multicalc.basiccalcfragment.save");
      memoryText.setText(str);
      savedInstanceState.keySet();
      Log.v("restoring", "text restored");}else Log.v("restoring","text not restored");

    } 

日志说文本已保存然后恢复,但在应用程序中,文本视图在旋转屏幕后被清除。我在 android 编程方面还是新手,所以也许我错过了一些东西。 有人可以帮我吗?

我厌倦了将 setRetainInstance 与 false 和 true 一起使用,但它没有帮助。我的 onCreateView 也是这样的:

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState)
{
      View view = inflater.inflate(R.layout.basic_calculator, container, false);    

     setRetainInstance(true);
      editText = (EditText) view.findViewById(R.id.txtInput);
      memoryText = (TextView) view.findViewById(R.id.txtMemory);

     //setting listeners for buttins from layout here

        disableSoftInputFromAppearing(editText);
        Log.v("basicCreate", "created basic");
      return view;
}

我不知道这是否重要,但我的 MainActivity 是:

@Override
protected void onCreate(Bundle savedInstanceState) {
    //setTheme(R.style.Sherlock___Theme_DarkActionBar);
    setContentView(R.layout.activity_main);
    actionBar = getSupportActionBar();
    super.onCreate(savedInstanceState);
       getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
       getSupportActionBar().setDisplayShowHomeEnabled(false);
       getSupportActionBar().setDisplayShowTitleEnabled(false); 

       tab = actionBar.newTab().setTabListener(new MyTabListener<BasicCalcFragment>(this, "Basic",BasicCalcFragment.class));
       tab.setText("Basic");
       actionBar.addTab(tab);
       tab = actionBar.newTab().setTabListener(new MyTabListener<ScientificCalcFragment>(this, "Scientific",ScientificCalcFragment.class));
       tab.setText("Scientific");
       actionBar.addTab(tab);
       tab = actionBar.newTab().setTabListener(new MyTabListener<ConverterFragment>(this, "Converter",ConverterFragment.class));
       tab.setText("Converter");
       actionBar.addTab(tab);
    if(savedInstanceState!=null){
        actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tabState"));
    }


}

由于 cYrixmorten 的响应,问题得以解决;)

【问题讨论】:

    标签: android fragment savestate


    【解决方案1】:

    这似乎是 SherlockFragment 的问题,或者至少很难找到解决方案。

    幸运的是,您只想保存和创建简单数据,所以我的建议是保存到 sharedPreference:

    将字符串保存在共享首选项中,然后在应用中的任何位置再次检索它。

    public class PreferencesData {
    
        public static void saveString(Context context, String key, String value) {
            SharedPreferences sharedPrefs = PreferenceManager
                    .getDefaultSharedPreferences(context);
            sharedPrefs.edit().putString(key, intValue).commit();
        }
    
        public static int getString(Context context, String key) {
            SharedPreferences sharedPrefs = PreferenceManager
                    .getDefaultSharedPreferences(context);
            return sharedPrefs.getString(key, defaultValue);
        }
    }
    

    用法:

    PreferencesData.saveString(context, "mynote", "Sherlock is weird");
    // retrieve
    String note = PreferencesData.getString(context, "mynote");
    

    使用它来保存暂停时的字符串,并在 onActivityCreated 中重新创建它

    希望对你有帮助

    【讨论】:

    • 我正在 onActivityCreated 检索字符串
    【解决方案2】:

    为此,您必须使用覆盖的“onCreate”或“onCreateView”方法。

    【讨论】:

      猜你喜欢
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      相关资源
      最近更新 更多