旋转期间TextViews丢失

时间:2014-02-18 07:20:51

标签: android android-fragments textview

我已经尝试了一个我能找到的解决方案而且没有任何运气。我有多个动态textViews,不仅可以更改文本,还可以更改alpha,strike-through等格式...我找不到通过屏幕旋转保存此信息的可靠方法。我是自学成才,新的头脑旋转......

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new UIrFragment())
                .commit();
    }
}

}

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    setRetainInstance(true);
    Log.i(Tag, "onCreateView");
    return rootView;
}


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

        ImageView background = (ImageView) getView().findViewById(R.id.background);
        background.setImageResource(R.drawable.starrynightblurry);
        setupKeyBoard(); // multiple textViews get set up here. Want to save this 
        setupGame();// same here

    }

3 个答案:

答案 0 :(得分:0)

activity_main.xml

中添加以下行
<activity android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation" >

答案 1 :(得分:0)

将屏幕方向固定为 AndroidManifest.xml 中的横向或纵向,如下所示:

 android:screenOrientation="portrait"

或像这样处理configChanges:

 android:configChanges="orientation|keyboardHidden|smallestScreenSize|uiMode"
应将

添加为清单中<activity>标记的属性。 configChanges用于指定活动将自行处理的配置更改。

编辑:要处理片段中的配置更改,请参阅this question

答案 2 :(得分:0)

显然,当使用 setRetainInstance(true)时,onRestoreInstanceState的Bundle为空。

中存储文字视图
@Override
public void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    //store here
}

从此处恢复文字视图

@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
    super.onRestoreInstanceState(savedInstanceState);        
    // restore here
}

希望这可以帮助你