Android 使用onSaveInstanceState()以片段形式保存视图状态
有许多关于onSaveInstanceState()未被调用等问题,但我无法找到这个特定问题的答案: 初始情况 使用Android Studio创建的基础应用程序,包含默认的NavDrawer实现。只是添加了一个按钮作为测试 意图 测试保存按钮和文本视图的可见性和文本 问题 如果Oretation更改,则使用onSaveInstanceState保存的值对对象没有影响 我尝试过的与视图/对象相关的一切都没有效果 编辑 我认为这是因为片段“创建”->新实例的方式。我如何将数据传递给这样一个“重新创建”的片段Android 使用onSaveInstanceState()以片段形式保存视图状态,android,android-fragments,Android,Android Fragments,有许多关于onSaveInstanceState()未被调用等问题,但我无法找到这个特定问题的答案: 初始情况 使用Android Studio创建的基础应用程序,包含默认的NavDrawer实现。只是添加了一个按钮作为测试 意图 测试保存按钮和文本视图的可见性和文本 问题 如果Oretation更改,则使用onSaveInstanceState保存的值对对象没有影响 我尝试过的与视图/对象相关的一切都没有效果 编辑 我认为这是因为片段“创建”->新实例的方式。我如何将数据传递给这样一个“重新创
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
private Button btnConnect;
private ProgressBar progress;
/**
* Returns a new instance of this fragment for the given sectionausgangssituation
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
btnConnect = (Button) rootView.findViewById(R.id.conov_btn_connect);
progress = (ProgressBar) rootView
.findViewById(R.id.conov_progress_connection);
// StatusLabel.setText(BluetoothController.get().getStatus().getMessage());
btnConnect.setOnClickListener(this);
if (savedInstanceState != null) {
Toast.makeText(getActivity(), "## test ##",
Toast.LENGTH_LONG).show();
btnConnect.setText(savedInstanceState.getString("savText"));
} else {
progress.setVisibility(View.GONE);
}
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("savText", "Hallo");
}