簡體   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