文章目录
静态添加
1)创建 Fragment 使用的 xml布局文件
2)新建一个类继承自 Fragment,重写 onCreate 方法
3)在需要使用 Fragment 的 xml 布局中申明 fragment 控件,并指定刚才新建的继承自 Fragment 的类
新建 fragment 的 xml 布局文件
mytest1\src\main\res\layout\fragment_layout_1.xml
<?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">
<TextView
android:id="@+id/fragment_1_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:text="hello"
android:background="@color/teal_200"
/>
</LinearLayout>
新建类MyFragment,继承自Fragment
mytest1\src\main\java\com\example\mytest1\MyFragment1.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class MyFragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_1, container, false);
TextView textView = view.findViewById(R.id.fragment_1_tv);
textView.setText("world");
return view;
// return super.onCreateView(inflater, container, savedInstanceState);
}
}
在 activity_main.xml 布局中引入 fragment
mytest1\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment_container"
android:layout_width="300dp"
android:layout_height="300dp"
android:name="com.example.mytest1.MyFragment1"
/>
</LinearLayout>
MainActivity
mytest1\src\main\java\com\example\mytest1\MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
}
示例代码
地址:test1
效果
动态添加 - add、replace、remove
新建 fragment 的 xml 布局文件
mytest2\src\main\res\layout\fragment_layout_1.xml
<?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">
<TextView
android:id="@+id/fragment_1_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:text="hello"
android:background="@color/teal_200"
/>
</LinearLayout>
mytest2\src\main\res\layout\fragment_layout_2.xml
<?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">
<TextView
android:id="@+id/fragment_2_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:text="Android"
android:background="@color/teal_200"
/>
</LinearLayout>
mytest2\src\main\res\layout\fragment_layout_3.xml
<?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">
<TextView
android:id="@+id/fragment_3_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:text="Java"
android:background="@color/teal_200"
/>
</LinearLayout>
新建类MyFragment,继承自Fragment
mytest2\src\main\java\com\example\mytest2\MyFragment1.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class MyFragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_1, container, false);
TextView textView = view.findViewById(R.id.fragment_1_tv);
textView.setText("world");
return view;
// return super.onCreateView(inflater, container, savedInstanceState);
}
}
mytest2\src\main\java\com\example\mytest2\MyFragment2.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class MyFragment2 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_2, container, false);
return view;
// return super.onCreateView(inflater, container, savedInstanceState);
}
}
mytest2\src\main\java\com\example\mytest2\MyFragment3.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class MyFragment3 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_3, container, false);
return view;
// return super.onCreateView(inflater, container, savedInstanceState);
}
}
activity_main.xml 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/add_fragment_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add fragment"
/>
<Button
android:id="@+id/replace_fragment2_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="replace fragment2"
/>
<Button
android:id="@+id/remove_fragment1_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remove fragment1"
/>
<RelativeLayout
android:id="@+id/fragment_container1"
android:layout_width="200dp"
android:layout_height="200dp"
/>
<RelativeLayout
android:id="@+id/fragment_container2"
android:layout_marginTop="10dp"
android:layout_width="200dp"
android:layout_height="200dp"
/>
</LinearLayout>
MainActivity
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MyFragment1 fragment1;
private MyFragment2 fragment2;
private MyFragment3 fragment3;
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button add_fragment_bt = findViewById(R.id.add_fragment_bt);
Button replace_fragment2_bt = findViewById(R.id.replace_fragment2_bt);
Button remove_fragment1_bt = findViewById(R.id.remove_fragment1_bt);
add_fragment_bt.setOnClickListener(this);
replace_fragment2_bt.setOnClickListener(this);
remove_fragment1_bt.setOnClickListener(this);
fragment1 = new MyFragment1();
fragment2 = new MyFragment2();
fragment3 = new MyFragment3();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_fragment_bt:
if (!fragment1.isAdded()) {
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container1, fragment1).commit();
}
if (!fragment2.isAdded()) {
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container2, fragment2).commit();
}
break;
case R.id.replace_fragment2_bt:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container2, fragment3).commit();
break;
case R.id.remove_fragment1_bt:
getSupportFragmentManager().beginTransaction().remove(fragment1).commit();
break;
default:
break;
}
}
}
示例代码
地址:test2
效果
模拟返回栈
达到效果:按下 Back 键后,回到上一个Fragment
新建 fragment 的 xml 布局文件
mytest3\src\main\res\layout\fragment_layout_1.xml
<?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">
<TextView
android:id="@+id/fragment_1_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:text="hello"
android:background="@color/teal_200"
/>
</LinearLayout>
mytest3\src\main\res\layout\fragment_layout_2.xml
<?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">
<TextView
android:id="@+id/fragment_2_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:text="Android"
android:background="@color/teal_200"
/>
</LinearLayout>
新建类MyFragment,继承自Fragment
mytest3\src\main\java\com\example\mytest3\MyFragment1.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class MyFragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_1, container, false);
return view;
// return super.onCreateView(inflater, container, savedInstanceState);
}
}
mytest3\src\main\java\com\example\mytest3\MyFragment2.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class MyFragment2 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_2, container, false);
return view;
// return super.onCreateView(inflater, container, savedInstanceState);
}
}
activity_main.xml 布局
mytest3\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/add_fragment_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add fragment"
/>
<Button
android:id="@+id/replace_fragment2_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="replace fragment2"
/>
<RelativeLayout
android:id="@+id/fragment_container1"
android:layout_width="200dp"
android:layout_height="200dp"
/>
</LinearLayout>
MainActivity
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MyFragment1 fragment1;
private MyFragment2 fragment2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button add_fragment_bt = findViewById(R.id.add_fragment_bt);
Button replace_fragment2_bt = findViewById(R.id.replace_fragment2_bt);
add_fragment_bt.setOnClickListener(this);
replace_fragment2_bt.setOnClickListener(this);
fragment1 = new MyFragment1();
fragment2 = new MyFragment2();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_fragment_bt:
if (!fragment1.isAdded()) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container1, fragment1)
.addToBackStack(null)
.commit();
}
break;
case R.id.replace_fragment2_bt:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container1, fragment2)
.addToBackStack(null)
.commit();
break;
default:
break;
}
}
}
示例代码
地址:test3
效果
Fragment的生命周期
当 MyFragment1 被添加到屏幕上时,会依次执行:
onAttach—>onCreate—>onCreateView—>onViewCreated—>onActivityCreated—>onStart—>onResume
当替换为 MyFragment2 时,如果使用 addToBackStack 时,会依次执行:
onPause—>onStop—>onDestroyView
当点击 Back 键时,会依次执行:
onCreateView—>onViewCreated—>onActivityCreated—>onStart—>onResume
当再次点击 Back 键时,会依次执行:
onPause—>onStop—>onDestroyView—>onDestroy—>onDetach
如果执行没有使用 addToBackStack 时,会直接执行:onDestroy—>onDetach
新建 fragment 的 xml 布局文件
mytest4\src\main\res\layout\fragment_layout_1.xml
<?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">
<TextView
android:id="@+id/fragment_1_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:text="hello"
android:background="@color/teal_200"
/>
</LinearLayout>
mytest4\src\main\res\layout\fragment_layout_2.xml
<?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">
<TextView
android:id="@+id/fragment_2_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:text="Android"
android:background="@color/teal_200"
/>
</LinearLayout>
新建类MyFragment,继承自Fragment
mytest4\src\main\java\com\example\mytest4\MyFragment1.java
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class MyFragment1 extends Fragment {
private static final String TAG = "AAAAAAAAAAA";
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
Log.d(TAG, "onAttach: ");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: ");
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: ");
View view = inflater.inflate(R.layout.fragment_layout_1, container, false);
return view;
// return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "onViewCreated: ");
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated: ");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart: ");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume: ");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause: ");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop: ");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "onDestroyView: ");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG, "onDetach: ");
}
}
mytest4\src\main\java\com\example\mytest4\MyFragment2.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class MyFragment2 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_2, container, false);
return view;
// return super.onCreateView(inflater, container, savedInstanceState);
}
}
activity_main.xml 布局
mytest4\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/add_fragment_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add fragment"
/>
<Button
android:id="@+id/replace_fragment2_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="replace fragment2"
/>
<RelativeLayout
android:id="@+id/fragment_container1"
android:layout_width="200dp"
android:layout_height="200dp"
/>
</LinearLayout>
MainActivity
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final String TAG = "BBBBBBBBBB";
private MyFragment1 fragment1;
private MyFragment2 fragment2;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "MainActivity onCreate: ");
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button add_fragment_bt = findViewById(R.id.add_fragment_bt);
Button replace_fragment2_bt = findViewById(R.id.replace_fragment2_bt);
add_fragment_bt.setOnClickListener(this);
replace_fragment2_bt.setOnClickListener(this);
fragment1 = new MyFragment1();
fragment2 = new MyFragment2();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_fragment_bt:
if (!fragment1.isAdded()) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container1, fragment1)
.commit();
}
break;
case R.id.replace_fragment2_bt:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container1, fragment2)
// .addToBackStack(null)
.commit();
break;
default:
break;
}
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "MainActivity onStart: ");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "MainActivity onResume: ");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "MainActivity onPause: ");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "MainActivity onStop: ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "MainActivity onDestroy: ");
}
}
示例代码
地址:test4
Activity 传值给 Fragment
方法1:通过调用 Fragment 中 set 方法传数据
方法2:通过 Bundle 的方式,传递数据
Fragment的xml布局
mytest5\src\main\res\layout\fragment_layout_1.xml
<?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">
<TextView
android:id="@+id/fragment_1_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:text="hello"
android:background="@color/teal_200"
/>
</LinearLayout>
新建Fragment类
mytest5\src\main\java\com\example\mytest5\MyFragment1.java
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class MyFragment1 extends Fragment {
private static final String TAG = "AAAAA";
// 方法1
private String text;
private String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
// 方法1
Log.d(TAG, "Activity 传值给 Fragment, method1 - " + getText());
// 方法2
if (getArguments() != null) {
Log.d(TAG, "Activity 传值给 Fragment, method2 - " + getArguments().getString("hiBoy"));
}
View view = inflater.inflate(R.layout.fragment_layout_1, container, false);
return view;
// return super.onCreateView(inflater, container, savedInstanceState);
}
}
activity_main.xml 布局
mytest5\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/add_fragment_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add fragment"
/>
<RelativeLayout
android:id="@+id/fragment_container1"
android:layout_width="200dp"
android:layout_height="200dp"
/>
</LinearLayout>
MainActivity
mytest5\src\main\java\com\example\mytest5\MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MyFragment1 fragment1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button add_fragment_bt = findViewById(R.id.add_fragment_bt);
add_fragment_bt.setOnClickListener(this);
fragment1 = new MyFragment1();
// Activity 传值给 Fragment, 方法1
fragment1.setText("new world!");
// Activity 传值给 Fragment, 方法2
Bundle bundle = new Bundle();
bundle.putString("hiBoy", "this is new world!");
fragment1.setArguments(bundle);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_fragment_bt:
if (!fragment1.isAdded()) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container1, fragment1)
.addToBackStack(null)
.commit();
}
break;
default:
break;
}
}
}
示例代码
地址:test5
Fragment 传值给 Activity
方法1:获取到 MainActivity,调用 Activity 的 set 方法进行传递数据
方法2:通过回调的方式,在 Fragment 中设置 interface,在 Activity 中进行进行接收传递的数据
Fragment的xml布局
mytest6\src\main\res\layout\fragment_layout_1.xml
<?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">
<TextView
android:id="@+id/fragment_1_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:text="hello"
android:background="@color/teal_200"
/>
</LinearLayout>
新建Fragment类
mytest6\src\main\java\com\example\mytest6\MyFragment1.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class MyFragment1 extends Fragment {
// 方法2 - 回调的方式
interface MyFragmentInterface {
void setText(String text);
}
MyFragmentInterface myFragmentInterface;
void setMyFragmentInterface(MyFragmentInterface mInterface) {
this.myFragmentInterface = mInterface;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
// Fragment 传值给 Activity, 方法1
MainActivity activity = (MainActivity) getActivity();
activity.setDesc("hello world!");
// Fragment 传值给 Activity, 方法2
myFragmentInterface.setText("hello Android!");
View view = inflater.inflate(R.layout.fragment_layout_1, container, false);
return view;
// return super.onCreateView(inflater, container, savedInstanceState);
}
}
activity_main.xml 布局
mytest6\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/add_fragment_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add fragment"
/>
<RelativeLayout
android:id="@+id/fragment_container1"
android:layout_width="200dp"
android:layout_height="200dp"
/>
<Button
android:id="@+id/test_get_value_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="get value"/>
</LinearLayout>
MainActivity
mytest6\src\main\java\com\example\mytest6\MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "BBBBBBB";
private MyFragment1 fragment1;
// Fragment 传值给 Activity, 方法1
private String desc;
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button add_fragment_bt = findViewById(R.id.add_fragment_bt);
add_fragment_bt.setOnClickListener(this);
Button test_get_value_bt = findViewById(R.id.test_get_value_bt);
test_get_value_bt.setOnClickListener(this);
fragment1 = new MyFragment1();
fragment1.setMyFragmentInterface(new MyFragment1.MyFragmentInterface() {
@Override
public void setText(String text) {
Log.d(TAG, "getText: " + text);
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_fragment_bt:
if (!fragment1.isAdded()) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container1, fragment1)
.addToBackStack(null)
.commit();
}
break;
case R.id.test_get_value_bt:
// 方法1
Log.d(TAG, "getDesc: " + getDesc());
break;
default:
break;
}
}
}
示例代码
地址:test6
方法
FragmentActivity
getFragmentManager() @deprecated
获取到 FragmentManager,该方法 @deprecated,推荐使用 getSupportFragmentManager
/** * Return the FragmentManager for interacting with fragments associated * with this activity. * * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()} */
@Deprecated
public FragmentManager getFragmentManager() {
return mFragments.getFragmentManager();
}
getSupportFragmentManager()
获取到 FragmentManager
/** * Return the FragmentManager for interacting with fragments associated * with this activity. */
@NonNull
public FragmentManager getSupportFragmentManager() {
return mFragments.getSupportFragmentManager();
}
FragmentManager
findFragmentById
获取 Fragment 实例
MyFragment1 myFragment1 = (MyFragment1) getSupportFragmentManager().findFragmentById(R.id.fragment_container1);
beginTransaction
开启事务
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
FragmentTransaction
add
添加 Fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container1, fragment1);
replace
替换 Fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container2, fragment3);
remove
移除 Fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction()
.remove(fragment1);
commit
提交事务
transaction.commit();
addToBackStack
将一个事务添加到返回栈中,当点击 Back 键时,返回上一个 Fragment。
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container1, fragment2)
.addToBackStack(null)
.commit();
Fragment
getActivity
得到和当前碎片相关联的活动实例。
MainActivity activity = (MainActivity) getActivity();
isAdded
判断 Fragment 是否被添加到 Activity
fragment1.isAdded()
文章评论