碎片的onCreateView被多次调用。

huangapple go评论79阅读模式
英文:

fragment's onCreateView called multiple times

问题

So I have a Settings fragment whose `onCreateView` gets called multiple times. I have ViewPager which is inside of the fragment's layout and the fragment launches by clicking on the settings icon from options menu in toolbar.

And I cannot just move the ViewPager to the activity because I want to be able to swipe back to the Previous Fragment. 

Also, I tried removing the fragments in the backstack using 

```java
for(int i=0;i< Objects.requireNonNull(fm).getBackStackEntryCount();++i){
      fm.popBackStackImmediate();
   }

but that did not help at all.

Fragment code:

public class SettingsFragment extends Fragment {
    
    public SettingsFragment() {
   
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Deprecated
    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        final View rootview = inflater.inflate(R.layout.fragment_settings, container, false);
        final CustomViewPager viewPager=rootview.findViewById(R.id.SettingsFragmentViewPager);
        viewPager.setAdapter(new Adapter(getFragmentManager()));
        viewPager.setOffscreenPageLimit(2);
        viewPager.setCurrentItem(1);
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    viewPager.setPagingEnabled(false);
                    FragmentManager fm=getFragmentManager();
                    for(int i=0;i< Objects.requireNonNull(fm).getBackStackEntryCount();++i){
                        fm.popBackStackImmediate();
                    }
                    viewPager.removeOnPageChangeListener(this);
                }
            }
        });
        return rootview;
    }
}

Fragment's xml:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="#fff" android:animationCache="true"
    android:id="@+id/SettingsFragmentContainer"
    android:clickable="true"
    android:drawingCacheQuality="high" android:focusable="true">

    <!-- Rest of the XML content -->

</androidx.constraintlayout.widget.ConstraintLayout>

The adapter I am using for ViewPager:

public class Adapter extends FragmentStatePagerAdapter {

    public Adapter(FragmentManager fragmentManager){
        super(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }

    @NotNull
    @Override
    public Fragment getItem(int position) {
        if(position==0){
            return new BlankFragment();
        }

        if(position==1){
            return new SettingsFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }
}

I see that in my adapter I am also returning SettingsFragment. Is there a way to return the fragment that was created when I clicked the settings button in the toolbar?

Btw, I add the fragment using this:

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
        case R.id.updateId:
            Log.d("MAIN ACTIVITY---","ADDING FRAGMENT...");
            FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
            ft.setCustomAnimations(R.anim.push_right_in,0);
            ft.add(R.id.MainActivityConstraint,new SettingsFragment());
            ft.commit();
            Log.d("ACTIVITY---","Fragment added...");
            break;
    }
}
英文:

So I have a Settings fragment whose onCreateView gets called multiple times. I have ViewPager which is inside of the fragment's layout and the fragment launches by a clicking on the settings icon from options menu in toolbar.

And I cannot just move the ViewPager to the activity because I want to be able to swipe back to the Previous Fragment.

Also,I tried removing the fragments in the backstack using

for(int i=0;i&lt; Objects.requireNonNull(fm).getBackStackEntryCount();++i){
      fm.popBackStackImmediate();
   }

but that did not help at all.

Fragment code:

public class SettingsFragment extends Fragment {
    
    public SettingsFragment() {
   
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    @Deprecated
    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        final View rootview = inflater.inflate(R.layout.fragment_settings, container, false);
        final CustomViewPager viewPager=rootview.findViewById(R.id.SettingsFragmentViewPager);
        viewPager.setAdapter(new Adapter(getFragmentManager()));
        viewPager.setOffscreenPageLimit(2);
        viewPager.setCurrentItem(1);
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    viewPager.setPagingEnabled(false);
                    FragmentManager fm=getFragmentManager();
                    for(int i=0;i&lt; Objects.requireNonNull(fm).getBackStackEntryCount();++i){
                        fm.popBackStackImmediate();
                    }
                    viewPager.removeOnPageChangeListener(this);
                }
            }


        });
        return rootview;
       
    }

Fragment's xml:

&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;

                                                   xmlns:tools=&quot;http://schemas.android.com/tools&quot;
                                                   xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
                                                   android:layout_height=&quot;match_parent&quot;
                                                   android:layout_width=&quot;match_parent&quot;
                                                   android:background=&quot;#fff&quot; android:animationCache=&quot;true&quot;
                                                   android:id=&quot;@+id/SettingsFragmentContainer&quot;
                                                   android:clickable=&quot;true&quot;
                                                   android:drawingCacheQuality=&quot;high&quot; android:focusable=&quot;true&quot;&gt;


        &lt;androidx.appcompat.widget.Toolbar
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:background=&quot;?attr/colorPrimary&quot;
                android:theme=&quot;?attr/actionBarTheme&quot;
                android:minHeight=&quot;?attr/actionBarSize&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot;
                app:layout_constraintStart_toStartOf=&quot;parent&quot;
                android:id=&quot;@+id/SettingsActivityToolbar&quot; app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                app:layout_constraintHorizontal_bias=&quot;1.0&quot;/&gt;
        &lt;TextView
                android:text=&quot;Settings&quot;
                android:layout_width=&quot;65dp&quot;
                android:layout_height=&quot;23dp&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot;
                app:layout_constraintStart_toStartOf=&quot;parent&quot; android:layout_marginTop=&quot;18dp&quot;
                app:layout_constraintEnd_toEndOf=&quot;parent&quot; android:id=&quot;@+id/Settings_textview&quot;
                android:textAppearance=&quot;@style/ActionBar.nameText&quot;/&gt;
        &lt;ImageButton
                android:layout_width=&quot;46dp&quot;
                android:layout_height=&quot;34dp&quot; app:srcCompat=&quot;@drawable/settings_icon_back&quot;
                android:id=&quot;@+id/backPressButton&quot;
                android:rotation=&quot;180&quot; android:backgroundTint=&quot;#fff&quot;
                android:background=&quot;@null&quot;
                app:layout_constraintStart_toStartOf=&quot;@+id/SettingsActivityToolbar&quot;
                android:layout_marginStart=&quot;2dp&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot;
                android:layout_marginTop=&quot;16dp&quot; android:onClick=&quot;click&quot; android:drawingCacheQuality=&quot;high&quot;/&gt;
        &lt;TextView
                android:text=&quot;&quot;
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;0.1dp&quot; app:layout_constraintStart_toStartOf=&quot;parent&quot;
                app:layout_constraintEnd_toEndOf=&quot;parent&quot; android:id=&quot;@+id/linebreak1&quot; android:layout_marginTop=&quot;10dp&quot;
                app:layout_constraintTop_toBottomOf=&quot;@+id/backPressButton&quot; android:background=&quot;#A1A8AC&quot;/&gt;
        &lt;TextView
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;0.1dp&quot; android:id=&quot;@+id/linebreak2&quot;
                android:background=&quot;#A1A8AC&quot;
                app:layout_constraintStart_toStartOf=&quot;parent&quot;
                app:layout_constraintEnd_toEndOf=&quot;parent&quot; android:layout_marginTop=&quot;7dp&quot;
                app:layout_constraintTop_toBottomOf=&quot;@+id/switch4&quot;/&gt;
        &lt;Switch
                android:text=&quot;Test Test&quot;
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;47dp&quot; android:id=&quot;@+id/switch4&quot;
                android:textAppearance=&quot;@style/ActionBar.nameText&quot; app:layout_constraintStart_toStartOf=&quot;parent&quot;
                android:layout_marginStart=&quot;25dp&quot; android:layout_marginEnd=&quot;25dp&quot;
                app:layout_constraintEnd_toEndOf=&quot;parent&quot; android:thumbTint=&quot;@drawable/switch_thumb_selector&quot;
                android:trackTint=&quot;@drawable/switch_track_selector&quot; android:layout_marginTop=&quot;18dp&quot;
                app:layout_constraintTop_toBottomOf=&quot;@+id/backPressButton&quot; tools:ignore=&quot;UseSwitchCompatOrMaterialXml&quot;/&gt;
        &lt;TextView
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;67dp&quot; android:id=&quot;@+id/update&quot;
                app:layout_constraintStart_toStartOf=&quot;parent&quot;
                app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                android:clickable=&quot;true&quot;
                android:textAppearance=&quot;@style/ActionBar.nameText&quot; app:layout_constraintHorizontal_bias=&quot;0.0&quot;
                android:background=&quot;?attr/selectableItemBackground&quot; android:layout_marginTop=&quot;16dp&quot;
                app:layout_constraintTop_toBottomOf=&quot;@+id/switch4&quot; android:onClick=&quot;onClickUpdate&quot;
                android:focusable=&quot;true&quot;/&gt;
        &lt;TextView
                android:text=&quot;test test&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:clickable=&quot;true&quot;
                android:layout_height=&quot;0dp&quot; android:id=&quot;@+id/enter_version&quot;
                app:layout_constraintTop_toTopOf=&quot;@+id/update&quot;
                android:layout_marginTop=&quot;36dp&quot; android:layout_marginEnd=&quot;206dp&quot;
                app:layout_constraintEnd_toEndOf=&quot;@+id/update&quot; android:focusable=&quot;true&quot;/&gt;
        &lt;TextView
                android:text=&quot;Check Update&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:clickable=&quot;true&quot;
                android:id=&quot;@+id/update_textview&quot;
                android:textAppearance=&quot;@style/ActionBar.nameText&quot; android:layout_marginEnd=&quot;228dp&quot;
                app:layout_constraintEnd_toEndOf=&quot;@+id/update&quot; app:layout_constraintTop_toTopOf=&quot;@+id/update&quot;
                android:layout_marginTop=&quot;10dp&quot; android:focusable=&quot;true&quot;/&gt;
        &lt;com.testapp.test2.CustomViewPager
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;0dp&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot;
                app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
                app:layout_constraintEnd_toEndOf=&quot;parent&quot; app:layout_constraintVertical_bias=&quot;0.58000004&quot;
                app:layout_constraintStart_toStartOf=&quot;parent&quot; android:id=&quot;@+id/SettingsFragmentViewPager&quot;
                /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

The adapter I am using for ViewPager:

public class Adapter extends FragmentStatePagerAdapter {

    public Adapter(FragmentManager fragmentManager){
        super(fragmentManager,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);

    }


    @NotNull
    @Override
    public Fragment getItem(int position) {
        if(position==0){
            return new BlankFragment();
        }

        if(position==1){
            return new SettingsFragment();
        }


        return null;
        }




    @Override
    public int getCount() {
        return 2;
    }


}

I see that in my adapter I am also returning SettingsFragment.Is there a way to return the fragment that was created when I clicked the settings button in the toolbar?

Btw, I add the fragment using this:

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
            case R.id.updateId:
                Log.d(&quot;MAIN ACTIVITY---&quot;,&quot;ADDING FRAGMENT...&quot;);
                FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
                ft.setCustomAnimations(R.anim.push_right_in,0);
                ft.add(R.id.MainActivityConstraint,new SettingsFragment());
                ft.commit();
                Log.d(&quot;ACTIVITY---&quot;,&quot;Fragment added...&quot;);
                break;

               }
}

答案1

得分: 0

我决定使用一个名为 swipebacklayout 的外部库,而不是 ViewPager。
你可以在这里查看它:https://github.com/gongwen/swipebacklayout

英文:

I decided to use an external library called swipebacklayout instead of ViewPager.
You can check it out here. https://github.com/gongwen/swipebacklayout

huangapple
  • 本文由 发表于 2020年10月20日 21:32:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64446252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定