旋转屏幕时保存的TextView的片段 [英] Saving textview in a fragment when rotating screen

查看:100
本文介绍了旋转屏幕时保存的TextView的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,节约型,然后从TextView的在sherlockframent连接作为一个选项卡到动作条,当我旋转屏幕恢复文本。我重写的onPause()和onActivityCreated(包savedInstanceState)梅托德在敬爱code:

I have problem with saving and then restoring text from TextView in sherlockframent attached as a tab into a actionbar when i rotate a screen. I override onPause() and onActivityCreated(Bundle savedInstanceState) metod as in belowed code:

@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");

    } 

日志是说文本保存,然后恢复,但在应用程序TextView的旋转屏幕后清除。我还是新的编程的机器人,也许我错过了什么。 有人可以帮我吗?

Logs are saying that text is saved and then restored, but in app the textview is cleared after rotating a screen. I'm still new in programing at android so maybe i missed something. Can someone help me?

我厌倦了使用setRetainInstance虚假和真实,但它并没有帮助。另外我onCreateView样子的:

I tired use setRetainInstance with false and true but it didnt helped. Also my onCreateView looks like that:

@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是:

I don't know if it is important but my MainActivity is:

@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响应;)

Problen solved thanks to cYrixmorten response;)

推荐答案

这似乎与SherlockFragment有问题,或至少很难找到一个解决方案。

It seems to be a problem with SherlockFragment, or atleast it is hard to find a solution.

幸运的是,你只是想保存,并创建简单的数据,所以我的建议是保存到共享preference:

Luckily you just want to save and create simple data, so my suggestion is saving to sharedPreference:

保存字符串中的共享preferences并检索它再次在任何地方你的应用程序。

Save string in shared preferences and retrieve it again anywhere in your app.

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

Use this to save the string on pause, and recreate it in onActivityCreated

希望这有助于

这篇关于旋转屏幕时保存的TextView的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆