- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我为 fragment 调用了 setRetainInstance(true)。我在 fragment 上添加了 EditText 和 TextView。旋转时,EditText 中的文字保留了下来,但 TextView 中的文字消失了。
我想我可以手动恢复TextView的文本,但我想知道为什么系统会自动恢复EditText的文本,而不是TextView的文本。我做错了什么吗?
重现步骤。
结果
EditText 有“android”,但 TextView 是空的。
MyFragment.java
public class MyFragment extends Fragment
{
private static final String TAG = "MyFragment";
EditText etInput;
Button btnTest1;
TextView tvMessage;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
Log.d(TAG, "onCreateView()");
View v= inflater.inflate(R.layout.my_fragment, container, false);
etInput = (EditText)v.findViewById(R.id.etInput);
btnTest1 = (Button)v.findViewById(R.id.btnTest1);
btnTest1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
tvMessage.setText(etInput.getText().toString());
Log.d(TAG, "btnTest1 was clicked");
}
});
tvMessage = (TextView) v.findViewById(R.id.tvMessage);
return v;
}
}
主 Activity .java
public class MainActivity extends AppCompatActivity
{
String TAG = this.getClass().getName();
@Override
protected void onCreate(Bundle savedInstanceState)
{
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment mf = fm.findFragmentById(R.id.placeholder);
if(mf == null)
{
Log.d(TAG, "creating new my fragment");
mf = new MyFragment();
mf.setRetainInstance(true);
fm.beginTransaction().add(R.id.placeholder, mf).commit();
}
}
}
我的 fragment .xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/etInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnTest1"
android:text="Test 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:text="Fragment Test"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/placeholder"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
</LinearLayout>
最佳答案
如果要启用 TextView
的自动恢复,需要在 xml 中将 freezesText
属性设置为 true
(或调用 setFreezesText(true)
).
对于TextView
,freezesText
默认值为false
,但是对于EditText
,默认值为真
。
来自 freezesText
的文档:
If set, the text view will include its current complete text inside of its frozen icicle in addition to meta-data such as the current cursor position. By default this is disabled; it can be useful when the contents of a text view is not stored in a persistent place such as a content provider. For EditText it is always enabled, regardless of the value of the attribute.
关于android - 为什么在调用 setRetainInstance(true) 时,TextView 的文本没有恢复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36625387/
我正在尝试使用 setRetainInstance(),但它似乎对我不起作用! =(。我将问题简化为最简单的版本: 我有一个带有 TextView 和 Button 的 fragment 。 Text
我正在使用无 View fragment 在我的 Activity 方向更改期间存储一些数据。它看起来大致是这样的: public class BoardActivity extends BaseAc
我有一个 DialogFragment 附加到 Activity,当屏幕旋转时,Activity 被销毁,DialogFragment 也被销毁,我想保留 DialogFragment 和我的Dial
假设我有一个 fragment A。它有一个实例变量 mViewPager 指向它的 ViewPager。 在 Fragment A 的 onCreate(Bundle) 中,我调用了 setReta
当您在 Fragment 上调用 setRetainInstance(true) 时,究竟会发生什么?文档几乎不存在,这似乎是一个非常重要的功能。具体来说,我想知道这个序列(我编造的)有多少是真的:
我正在使用一个具有 SearchActivity 的应用程序;搜索 Activity 只是“SearchFragment”的容器,“SearchFragment”在ActionBar中包含一个Sear
这让我受够了。无论我如何尝试,我都无法在设备旋转时保留 textview 的状态。 示例如下: activity_main.xml RotationFragmentDemo.
我用的是ListFragment 设置保留实例(真);不工作,我不知道为什么 savedInstanceState == null 我的资源 public class Fragment_Left
如果我有一个 Activity 承载一个带有 setRetainInstance(true) 的 Fragment 如果这个 Activity 在后台被杀死会发生什么,即另一个 Activity 在它
对所有 Fragment 使用 Fragment.setRetainInstance() 以摆脱处理 Fragment 是一种好习惯吗娱乐、保存实例状态等?为什么不呢? 最佳答案 是的,如果它们必须保
我试图在方向更改时保存一个 ListFragment 子类,因此我将 setRetainInstance(true) 添加到我的 onCreate 函数的末尾。我添加了一个 onSaveInstanc
背景 我有一个 fragment 的 Activity ,在创建时需要动画,但在方向改变时不需要。 fragment 被动态插入到布局中,因为它是抽屉导航式 Activity 的一部分。 问题 我想避
Here , Dianne 说通过 onRetainNonConfigurationInstance() 保留对象的旧方法现在已经过时了,因为您可以在配置更改时保留 Fragment 实例。 还有 h
我发现 Fragment#setRetainInstance(true) 令人困惑。这是从 Android Developer API 中提取的 Javadoc : public void setRe
我创建了一个包含4个片段的底部导航项目,并将setHasOptionsMenu(true)放在qponFragment的onCreate()中,以便在切换Fragments之后保持qponFragme
从文档中得知,setRetainInstance(boolean) 方法有助于在设备配置更改时保留所有 Activity 对象,但我阅读的大多数示例和文档都涉及 headless fragment 。
我通过替换在多个 fragment 之间切换。 fragment 也被添加到返回堆栈。 现在我希望在方向改变时不再创建 fragment 。我将 setRetainInstance(true) 放在它
我正在学习 Google 提供的高级 android 开发培训。这是 link .我遇到了这个:Added a static fragment in xml 并且在旋转设备时,所选单选按钮的状态将保留
我有一个包含单个子 fragment 的父 fragment , fragment 的 onAttach 和 onCreateView 方法都被记录下来用于调试目的。 当我以这种方式将父 fragme
如果我在这方面有任何错误,请纠正我。这是一个澄清问题,因为我没有看到它在任何地方明确写过。 在 Android 4 中,您可以在 Fragment 上调用 setRetainInstance(true
我是一名优秀的程序员,十分优秀!