繁体   English   中英

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

[英]Orientation Change Causes Series of Fragments in Activity Disappear and Do Not Apply Save Instance State

因此,我在开发项目中遇到了另一个障碍。

这次涉及到有关改变方向和保留其保存的实例状态的片段。 如果涉及到两个问题,但是请注意相同的问题催化剂(方向改变),请原谅我。 我会尽量详细,请多多包涵。

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

因此,我有3个片段,分别托管在一个实现导航抽屉的片段中。 逻辑流程如下:

Frag1->(按Next)-> Frag2->(按Next)-> 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捆绑包)确实可以正常工作,并且能够将数据传递给EditText。 但是,我还发现,在更改方向时,Activity还会调用onNavigationDrawerItemSelected(int position),正如您在其中的代码中所看到的那样,它擦除Frag1的FragmentManager并将其替换为不具有值的新Frag1卡还没有。

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

在此先感谢那些会回答的人。 :)

很粗糙的想法

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM