如果我有3个片段,如frag-1,frag-2,frag-3,我确实在每个片段中都有3个片段的链接。每当我再次访问先前打开的片段时,我只需要重用或不重新创建新实例。就像我从frag-1开始然后我打开frag-2然后我打开frag-3再次打开frag-2然后我应该得到之前的实例而不是获得新的实例。 就像在活动中一样,我们可以使用启动模式,例如,单个任务不允许创建新对象,它将重用来自后栈的现有对象。
编辑: 添加了代码
package com.example.fragmentexample1;
import java.util.List;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.example.fragments.Fragment1;
import com.example.fragments.Fragment2;
import com.example.fragments.Fragment3;
public class MainActivity extends ActionBarActivity implements OnClickListener {
private Button btn1, btn2, btn3;
private Fragment1 frag1;
private Fragment2 frag2;
private Fragment3 frag3;
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
frag1 = new Fragment1();
frag2 = new Fragment2();
frag3 = new Fragment3();
}
@Override
public void onClick(View v) {
if (v.getId() == btn1.getId()) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.lin2, frag1);
transaction.addToBackStack(null);
transaction.commit();
} else if (v.getId() == btn2.getId()) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.lin2, frag2);
transaction.addToBackStack(null);
transaction.commit();
} else if (v.getId() == btn3.getId()) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.lin2, frag3);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
布局XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/tempList"
android:layout_width="match_parent"
android:layout_height="100dp" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OpenFragment2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OpenFragment3" />
<LinearLayout
android:id="@+id/lin2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
片段1的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment ONE!!!"
/>
<EditText
android:id="@+id/ed1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
/>
</LinearLayout>