方向更改导致活动中的碎片系列消失并且不应用保存实例状态

时间:2014-09-23 15:08:02

标签: java android android-fragments

所以我在我的开发项目中遇到了另一个障碍。

这次涉及到改变方向和持久保存实例状态的片段。如果它涉及两个问题,请原谅我,但它们涉及相同的问题催化剂,即方向改变。我会尽量详细说明,所以请耐心等待。

我有一堆片段使用单个活动作为主机。在我看来,我的片段按部分分组,尽管它们仍然是模块化的/可重复使用的代码。

所以我有3个片段托管在一个实现导航抽屉的片段中。逻辑流程如下:

  

Frag1 - > (按下一步) - > Frag2 - > (按下一步) - > Frag3

Frag2和Frag3的创建在FragmentManager中提交事务时调用addToBackStack()。 问题是当我在Frag2或Frag3中然后更改设备方向时,片段消失然后显示Frag1。

另一个问题是,当我在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();
}

用于在Activity.java中创建Frag2的代码段

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();
}

用于在Activity.java中创建Frag3的代码段

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();
    }
}

关于在方向改变时消失的碎片,我仍然无法找到可能的解决方案。

关于更改方向时保存的实例状态的持久性,我尝试在Frag1类中实现onSaveInstanceState(Bundle bundle),如下所示:

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。但是,我还发现Activity在更改方向时也会调用onNavigationDrawerItemSelected(int position),正如您在其中的代码中所看到的,擦除Frag1的FragmentManager并将其替换为没有值的新Frag1。卡还没有。

我想这里的基本问题是在这种情况下实现onSaveInstanceState(Bundle bundle)的最佳方法是什么?我尝试在Fragments中实现它,但我可能犯了一个错误?或者我应该在Activity中实现它,比如保存现有片段的实例?

提前感谢那些愿意回答的人。 :)

1 个答案:

答案 0 :(得分:0)

非常粗略的想法

在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 ??!!??!!??!!

}