質問

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

    } 

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?

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;
}

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


}

Problen solved thanks to cYrixmorten response;)

役に立ちましたか?

解決

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

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

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);
    }
}

Usage:

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

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

Hope this helps

他のヒント

You have to use overrided "onCreate" or "onCreateView" method for this purpose.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top