Fragment比Activity较轻量级,也可以提供与用户交互的界面并且有自己的生命周期,也不用在Manifest.xml中注册但它必须嵌套在Activity中使用。之前需要使用多个Activity显示的内容,现在可以用一个Activity嵌套多个Fragment来实现。
Fragment的生命周期不是从new Fragment()开始的,而是从Fragment增加到Activity开始的。
1.activity_main.xml
2.fragment_one.xml
3.fragment_two.xml
总体思路如下:
1.先创建类继承Fragment 准备布局 fragment实现onCreateView
2.显示fragment到activity
静态: 在xml布局文件中写标签
动态: FragmentManager 获得 FragmentTransaction
//添加fragment
fragmentTransaction.replace(R.id.line1,fragmentOne);
//把fragment加入到后台栈
fragmentTransaction.addToBackStack(“two”);
//从后台栈移除fragment
FragmentManager.popBackStack();
准备布局 fragment实现onCreateView
FragmentOne代码:
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one,null);
}
}
FragmentTwo代码:
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by sq on 2020/2/10.
*/
public class FragmentTwo extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two,null);
}
}
采用动态: FragmentManager 获得 FragmentTransaction
//添加fragment
fragmentTransaction.replace(R.id.line1,fragmentOne);
//把fragment加入到后台栈
fragmentTransaction.addToBackStack(“two”);
//从后台栈移除fragment
FragmentManager.popBackStack();
MainActivity代码
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button one,two;
FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.second);
fm = getFragmentManager();
fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
Log.i("message","@@"+fm.getBackStackEntryCount());
}
});
/*one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentOne fragmentOne = new FragmentOne();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
// p1 放在哪个布局里
fragmentTransaction.replace(R.id.line1,fragmentOne);
fragmentTransaction.commit();
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTwo fragmentTwo = new FragmentTwo();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
// p1 放在哪个布局里
fragmentTransaction.replace(R.id.line1,fragmentTwo);
fragmentTransaction.commit();
}
});*/
}
public void click(View view){
int id = view.getId();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
switch(id){
case R.id.one:
FragmentOne fragmentOne = new FragmentOne();
// p1 放在哪个布局里
fragmentTransaction.replace(R.id.line1,fragmentOne);
fragmentTransaction.addToBackStack("one");
break;
case R.id.second:
FragmentTwo fragmentTwo = new FragmentTwo();
// p1 放在哪个布局里
fragmentTransaction.replace(R.id.line1,fragmentTwo);
fragmentTransaction.addToBackStack("two");
break;
case R.id.back:
fm.popBackStack();
break;
}
fragmentTransaction.commit();
}
}