在方向更改后使用加载程序时保留 EditText 更改
Retaining EditText changes when using Loaders after orientation change
我有一个用于编辑项目的片段。我用加载程序加载所有数据,然后让用户编辑它。问题在于,当用户在 EditText
中输入一些数据然后旋转设备时,加载程序会重新加载数据并覆盖用户所做的所有更改。当然,当我注释掉 initLoader()
时,旋转后会保留 EditText 值。
在方向改变后停止加载器重新加载的常见模式有哪些?我能想到的最简单的解决方案是将某种标志变量放入 onSaveInstanceState()
并在 onLoadFinished()
中添加一个 if 语句以不重新加载数据,但我想知道是否有更好的解决方案.以下是我的片段中的简化代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_edit_project, container, false);
this.projectAddressInput = (EditText) view.findViewById(R.id.fragment_add_edit_project_address);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(0, null, this);
}
@Override
public Loader onCreateLoader(int id, Bundle args) {
return new CursorLoader(
getActivity(),
Project.buildProjectUri(this.projectId),
PROJECTION,
null,
null,
null
);
}
@Override
public void onLoadFinished(Loader loader, Object data) {
Cursor cursor = (Cursor) data;
if (cursor != null && cursor.moveToFirst()) {
this.projectAddressInput.setText(cursor.getString(cursor.getColumnIndex(Project.COLUMN_ADDRESS)));
}
}
@Override
public void onLoaderReset(Loader loader) {}
非常感谢任何建议。
在您的 AndroidManifest.xml
中声明 android:configChanges
以指示 Activity 管理器在配置更改时不要重新启动您的 activity(结果将重新加载您的 CursorLoader
):
<activity
...
android:configChanges="orientation|screenSize" />
If your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity.
参考:http://developer.android.com/guide/topics/resources/runtime-changes.html
我有一个用于编辑项目的片段。我用加载程序加载所有数据,然后让用户编辑它。问题在于,当用户在 EditText
中输入一些数据然后旋转设备时,加载程序会重新加载数据并覆盖用户所做的所有更改。当然,当我注释掉 initLoader()
时,旋转后会保留 EditText 值。
在方向改变后停止加载器重新加载的常见模式有哪些?我能想到的最简单的解决方案是将某种标志变量放入 onSaveInstanceState()
并在 onLoadFinished()
中添加一个 if 语句以不重新加载数据,但我想知道是否有更好的解决方案.以下是我的片段中的简化代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_edit_project, container, false);
this.projectAddressInput = (EditText) view.findViewById(R.id.fragment_add_edit_project_address);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(0, null, this);
}
@Override
public Loader onCreateLoader(int id, Bundle args) {
return new CursorLoader(
getActivity(),
Project.buildProjectUri(this.projectId),
PROJECTION,
null,
null,
null
);
}
@Override
public void onLoadFinished(Loader loader, Object data) {
Cursor cursor = (Cursor) data;
if (cursor != null && cursor.moveToFirst()) {
this.projectAddressInput.setText(cursor.getString(cursor.getColumnIndex(Project.COLUMN_ADDRESS)));
}
}
@Override
public void onLoaderReset(Loader loader) {}
非常感谢任何建议。
在您的 AndroidManifest.xml
中声明 android:configChanges
以指示 Activity 管理器在配置更改时不要重新启动您的 activity(结果将重新加载您的 CursorLoader
):
<activity
...
android:configChanges="orientation|screenSize" />
If your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity.
参考:http://developer.android.com/guide/topics/resources/runtime-changes.html