【发布时间】:2013-04-23 08:39:37
【问题描述】:
我正在尝试用活动做一些事情,但在一个片段内。 我所做的是使用活动:
旋转设备时首先停止活动重新启动
android:configChanges="keyboardHidden|orientation|screenSize"
在我的活动中添加:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
}
所以拿到activity并没有重启,而是重新加载main.xml,来使用layout-land
现在我有一个显示 viewpager 的活动,其中包含三个片段。 一切正常。旋转检测是在片段中
public class FRG_map_web extends Fragment {
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("myLogs", "Rotation");
}
问题是片段没有使用 setContentView(R.layout.main);这是代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frg.myFragment, null);
我尝试使用:
LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.frg.myFragment, null);
...
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.frg.myFragment, null);
...
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
view = inflater.inflate(R.layout.frg.myFragment, null);
...
LayoutInflater li = LayoutInflater.from(context);
和不同的方式,但总是没有成功 我无法正常充气。
谁能告诉我该怎么做?
在此先感谢,感谢您的帮助
问候
【问题讨论】:
-
你的问题一点都不清楚。您是否尝试在旋转后重新创建片段?
-
我的意图是旋转设备后不重启activity,而是fragment的onConfigurationChanged,重新加载布局xml
-
你会遇到
android:configChanges的问题,因为在大多数情况下,你不应该使用它。您没有考虑到许多类型的配置更改。onSavedInstanceState是你的朋友。 -
我在片段中使用位图,如果每次旋转设备时片段都会重新加载,我会遇到“内存不足”的问题。第一次加载是受控的,而不是其余的(如何旋转),所以我只需要加载一次
-
因为位图而覆盖
android:configChanges是一个糟糕的借口。您必须通过onSavedInstanceState在配置更改之间保留位图。
标签: android android-fragments layout-inflater