如何在片段之间切换而不必重新创建每个片段?

时间:2015-11-17 00:13:44

标签: java android xml android-fragments

我目前有5个可以切换的片段,但每次切换时,变为活动的片段都会重新创建。我想知道如何制作它,以便如果一个片段不再显示,它的状态仍然保存在后台,所以当我回到这个片段时,它仍然是如何离开的。这是我当前的代码..

activity_main.xml中:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/fragment_placeholder"
    android:layout_width="match_parent"
    android:layout_height="@dimen/zero"
    android:layout_weight="12"
    android:orientation="vertical">
</LinearLayout>

<LinearLayout
    android:id="@+id/theNav"
    android:layout_width="match_parent"
    android:layout_height="@dimen/zero"
    android:layout_weight="1"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/oneButton"
        android:layout_width="@dimen/zero"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#1A1A1A"
        android:src="@mipmap/one"
        android:onClick="onSelectFragment" />

    <ImageButton
        android:id="@+id/twoButton"
        android:layout_width="@dimen/zero"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#1A1A1A"
        android:src="@mipmap/two"
        android:onClick="onSelectFragment" />

    <ImageButton
        android:id="@+id/threeButton"
        android:layout_width="@dimen/zero"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#1A1A1A"
        android:src="@mipmap/three"
        android:onClick="onSelectFragment" />

    <ImageButton
        android:id="@+id/fourButton"
        android:layout_width="@dimen/zero"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#1A1A1A"
        android:src="@mipmap/four"
        android:onClick="onSelectFragment" />

    <ImageButton
        android:id="@+id/fiveButton"
        android:layout_width="@dimen/zero"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#1A1A1A"
        android:src="@mipmap/five"
        android:onClick="onSelectFragment" />

</LinearLayout>

</LinearLayout>

fragment_one.xml,fragment_two.xml,fragment_three.xml,fragment_four.xml,fragment_five.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
</LinearLayout>

FragmentOne.java,FragmentTwo.java,FragmentThree.java,FragmentFour.java,FragmentFive.java(每个都有各自的布局文件):

public class FragmentOne extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one, container, false);

        return view;
    }
}

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private ImageButton one;
    private ImageButton two;
    private ImageButton three;
    private ImageButton four;
    private ImageButton five;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        FragmentOne oneFragment = new FragmentOne();

        transaction.add(R.id.fragment_placeholder, oneFragment);
        transaction.commit();

        one = (ImageButton) findViewById(R.id.oneButton);
        two = (ImageButton) findViewById(R.id.twoButton);
        three = (ImageButton) findViewById(R.id.threeButton);
        four = (ImageButton) findViewById(R.id.fourButton);
        five = (ImageButton) findViewById(R.id.fiveButton);
    }

    public void onSelectFragment(View view) {
        Fragment newFragment;

        if (view == findViewById(R.id.oneButton)) {
            newFragment = new FragmentOne();
        } else if (view == findViewById(R.id.twoButton)) {
            newFragment = new FragmentTwo();
        } else if (view == findViewById(R.id.threeButton)) {
            newFragment = new FragmentThree();
        } else if (view == findViewById(R.id.fourButton)) {
            newFragment = new FragmentFour();
        } else if (view == findViewById(R.id.fiveButton)) {
            newFragment = new FragmentFive();
        } else {
            newFragment = new FragmentOne();
        }

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_placeholder, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

}

2 个答案:

答案 0 :(得分:0)

你可以使用FragmentPagerAdapter来管理你的片段,这是显示一些静态片段的一种方法。   您可以在此处获取更多信息:http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

答案 1 :(得分:0)

您可以使用FragmentTransaction的attach和detach方法,使用标记来识别片段。

这是一个例子:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    selectFragment( FragmentOne.class.getName() );

    one = (ImageButton) findViewById(R.id.oneButton);
    two = (ImageButton) findViewById(R.id.twoButton);
    three = (ImageButton) findViewById(R.id.threeButton);
    four = (ImageButton) findViewById(R.id.fourButton);
    five = (ImageButton) findViewById(R.id.fiveButton);
}

public void onSelectFragment(View view) {

    String className;

    if (view == findViewById(R.id.oneButton)) {
        className = FragmentOne.class.getName();
    } else if (view == findViewById(R.id.twoButton)) {
        className = FragmentTwo.class.getName();
    } else if (view == findViewById(R.id.threeButton)) {
        className = FragmentThree.class.getName();
    } else if (view == findViewById(R.id.fourButton)) {
        className = FragmentFour.class.getName();
    } else if (view == findViewById(R.id.fiveButton)) {
        className = FragmentFive.class.getName();
    } else {
        className = FragmentOne.class.getName();
    }

    selectFragment( className );
}

private void selectFragment( String fragClassName )
{
    // Init transaction
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    // Get current Fragment
    Fragment currentFragment = fragmentManager.findFragmentByTag( mCurrFragment );

    // If found and different from current required detach it
    if( currentFragment != null )
        fragmentTransaction.detach(currentFragment);

    // Attach (create if first time) new Fragment
    Fragment newFragment = fragmentManager.findFragmentByTag(fragClassName);
    if (newFragment != null)
    {
        fragmentTransaction.attach(newFragment);
    }
    else
    {
        newFragment = Fragment.instantiate(MainActivity.this, newFragmentClass);
        newFragment.setRetainInstance(true);
        fragmentTransaction.add( R.id.fragment_placeholder, newFragment, newFragmentClass );
    }

    // Commit
    fragmentTransaction.commit();
    mCurrFragment = fragClassName;
}

请注意配置更改。

FragmentManager将片段保留在活动娱乐上,但它再次调用所有生命周期方法。 如果你将setRetainInstance设置为true,它将被调用除onCreate / onDestroy之外的所有方法,因此你可以在那里创建你不想放弃的所有“重”的东西。