所以我在我的开发项目中遇到了另一个路障。
另一个问题是,当我在Frag1中,然后在EditText中键入一些文本,然后更改方向时,尽管实现了onSaveInstanceState(Bundle Bundle),文本还是会消失。
以下是我的相关代码片段:
在activity.java中创建Frag1的代码段
@Override
public void onNavigationDrawerItemSelected(int position) {
// init the fragment (with a default fragment, not null)
rootFragment = PlaceholderFragment.newInstance(position + 1);
// Position number from navigation sidebar starts from 0.
// Since position starts from 0, add 1 to match section number
// as implemented in {@link #onSectionAttached()}
switch (position) {
case 0: // Other section
rootFragment = PlaceholderFragment.newInstance(position + 1);
break;
case 1: // Frag1
rootFragment = new AddPointsFragment().newInstance(position + 1, "");
break;
case 2: // Other section
rootFragment = new CheckPointsFragment().newInstance(position + 1, "");
break;
default:
break;
}
// update the main primary content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
// clear all fragments from previous section from the back stack
fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
// replace all currently added fragments in container and replace with the new fragment
// don't add to back stack since these serve as "root" fragments
fragmentManager.beginTransaction()
.replace(R.id.container, rootFragment, rootFragment.getClass().getName())
.commit();
}
public void startAddPointsSuccessFragment(int sectionNumber, String cardNo, int points) {
// Create fragment and give it its arguments
// Since this is not a major fragment,
// I just used its major parent fragment (AddPointsFragment)
// section number to follow the action bar title
AddPointsSuccessFragment addPointsSuccessFragment =
new AddPointsSuccessFragment().newInstance(sectionNumber, cardNo, points);
// Replace whatever is in the container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, addPointsSuccessFragment)
.addToBackStack(AddPointsSuccessFragment.class.getName())
.commit();
}
public void onOkButtonAddPointsSuccessFragmentInteraction(int sectionNumber, String cardNo, int points) {
int minimumPoints = 10;
if (points < minimumPoints) {
// Go back to previous fragment (AddPointsFragment)
// by simulating a back press from host activity.
this.onBackPressed();
}
else {
// Create fragment and give it its arguments
// Since this is not a major fragment,
// I just used its major parent fragment (AddPointsFragment)
// section number to follow the action bar title
RedeemRewardFragment redeemRewardFragment =
new RedeemRewardFragment().newInstance(sectionNumber, cardNo, points);
// Replace whatever is in the container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, redeemRewardFragment)
.addToBackStack(RedeemRewardFragment.class.getName())
.commit();
}
}
AddPointsFragment.java的代码段
EditText cardNoEditText;
@Override
public void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putInt(ARG_SECTION_NUMBER, mSectionNumber);
bundle.putString(ARG_CARD_NO, cardNoEditText.getText().toString());
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mSectionNumber = getArguments().getInt(ARG_SECTION_NUMBER);
mCardNo = getArguments().getString(ARG_CARD_NO);
}
if (savedInstanceState != null) {
mSectionNumber = savedInstanceState.getInt(ARG_SECTION_NUMBER);
mCardNo = savedInstanceState.getString(ARG_CARD_NO);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_points, container, false);
cardNoEditText = (EditText) view.findViewById(R.id.cardNoEditText);
cardNoEditText.setText(mCardNo);
Button enterCardNoButton = (Button) view.findViewById(R.id.enterCardNoButton);
enterCardNoButton.setOnClickListener(this);
Button scanCardNoButton = (Button) view.findViewById(R.id.scanCardNoButton);
scanCardNoButton.setOnClickListener(this);
return view;
}
我尝试在代码中运行调试器,以了解在更改方向期间失败的持久性,经过检查,onSaveInstanceState(Bundle Bundle)确实工作了,并且能够将数据传递给EditText。但是,我还发现活动在改变方向时还调用onNavigationDrawerItemSelected(int position)(您可以在它的代码中看到,它擦除Frag1的FragentManager,并用一个还没有值为cardNo的新Frag1替换它。
我想这里的潜在问题是在这个场景中实现onSaveInstanceState(Bundle Bundle)的最佳方式是什么?我尝试在片段中实现它,但我可能犯了一个错误?或者我应该在活动中实现它,就像保存现有片段的实例一样?
提前感谢那些将回答的人。:)
非常粗略的想法
在oncreate??还是oncreateview???
if(savedInstanceState == null){
}else{
int positionx = savedInstanceState.getInt("myposition"); //setfragment myposition ??!!??!!??
myEdittext.setText(savedInstanceState.getString("myedittext")); //set text to edittext ??!!??!!??!!
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putInt("myposition", position);
savedInstanceState.putString("myedittext", myEdittext.getText().toString());
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
int positionx = savedInstanceState.getInt("myposition"); //setfragment myposition ??!!??!!??
myEdittext.setText(savedInstanceState.getString("myedittext")); //set text to edittext ??!!??!!??!!
}
activity_main_two.xml https://github.com/rjrogers/twopaneuitest/blob/master/app/src/main/res/layout/activit_main_two.xml fragment_steps.xml https://github.com/rjrogers/twopaneuitest/blob/master/app/sr
我有一个活动,将使用下面的方法动态添加片段。 FragmentA有一个文本视图。我的活动中有一个导航抽屉,我想根据在导航抽屉中单击的项目在片段(例如FragmentA、FragmentB和FragmentC)之间切换。当更改为另一个片段时,如何保存片段的状态。我已经实现了onSavedInstance(Bundle outState)和onActivityCreated(Bundle savedI
我试过configChanges,但显然它没有改变我的布局。我试着在onConfigurationChanged的FrameLayout容器中填充肖像和景观布局并替换它们,但似乎也不起作用。 如何处理需要加载不同布局同时保持片段状态不变的情况?这有可能吗?对于这种事情,正确的做法是什么?
我正在android应用程序中使用导航抽屉<每个片段都包含从internet获取数据并显示在自定义列表中的异步任务。 这是选择片段的代码: 但当我在片段之间切换时,一个异步任务再次开始加载数据<那么如何保存片段的状态呢 我在stackoverflow上尝试了所有可能的解决方案 请帮帮我
问题内容: 我想在使用Android的导航抽屉切换片段时保存片段的状态。如果该片段先前已加载,则不应刷新。可能吗? 问题答案: 要保持片段的状态,您必须在片段的内调用。它能做什么: 控制是否在活动重新创建期间保留片段实例(例如通过配置更改)。 这样可以保持活动重新创建的状态,但是在这种情况下,将不会重新创建活动,而是使用抽屉手动切换片段。在这种情况下,您不必在抽屉的click侦听器中创建新的片段,
我想在使用androids导航抽屉切换片段时保存片段的状态。如果片段先前已加载,则不应刷新。有可能吗?