Jetpack Navigation - 在 Fragment 中跳转到 Activity(4 种方式) - 详解

一、使用 startActivity 跳转

1、Fragment Layout
  • fragment_test.xml
<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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=".fragment.TestFragment">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TestFragment"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
  <Button
    android:id="@+id/btn_jump"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="跳转"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2、Activity Code
  • TestFragment.xml
public class TestFragment
extends Fragment {
public static final String TAG = TestFragment.class.
getSimpleName();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_test, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btnJump = view.findViewById(R.id.btn_jump);
btnJump.setOnClickListener(v ->
{
Intent intent = new Intent(getActivity(), Test2Activity.class)
;
startActivity(intent);
});
}
}
3、Activity Layout
  • activity_test1.xml
<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Test1Activity">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test1Activity"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
  <fragment
    android:name="com.my.navigation.fragment.TestFragment"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • activity_test2.xml
<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Test1Activity">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test2Activity"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
  <Button
    android:id="@+id/btn_back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="返回"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
4、Activity Code
  • Test1Activity.java
public class Test1Activity
extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_test1);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) ->
{
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
}
  • Test2Activity.java
public class Test2Activity
extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_test2);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) ->
{
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
Button btnBack = findViewById(R.id.btn_back);
btnBack.setOnClickListener(v ->
{
finish();
});
}
}

二、使用 Activity Result API 跳转

1、Fragment Code
  • TestFragment.java
ActivityResultLauncher<
Intent> activityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result ->
{
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
int num = data.getIntExtra("num", 0);
Log.i(TAG, "num: " + num);
}
}
);
Button btnJump = view.findViewById(R.id.btn_jump);
btnJump.setOnClickListener(v ->
{
Intent intent = new Intent(getActivity(), Test2Activity.class)
;
activityResultLauncher.launch(intent);
});
2、Activity Code
  • Test2Activity.java
Button btnBack = findViewById(R.id.btn_back);
btnBack.setOnClickListener(v ->
{
Intent intent = new Intent();
intent.putExtra("num", 100);
setResult(RESULT_OK, intent);
finish();
});

三、使用 startActivityForResult 方法跳转

1、Fragment Code
  • TestFragment.java
private static final int REQUEST_CODE = 100;
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
int num = data.getIntExtra("num", 0);
Log.i(TAG, "num: " + num);
}
}
}
Button btnJump = view.findViewById(R.id.btn_jump);
btnJump.setOnClickListener(v ->
{
Intent intent = new Intent(getActivity(), Test2Activity.class)
;
startActivityForResult(intent, REQUEST_CODE);
});
2、Activity Code
  • Test2Activity.java
Button btnBack = findViewById(R.id.btn_back);
btnBack.setOnClickListener(v ->
{
Intent intent = new Intent();
intent.putExtra("num", 100);
setResult(RESULT_OK, intent);
finish();
});

四、使用 Navigation 跳转

1、Fragment Layout
  • fragment_test.xml
<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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=".fragment.TestFragment">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TestFragment"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
  <Button
    android:id="@+id/btn_jump"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="跳转"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2、Fragment Code
  • TestFragment.java
public class TestFragment
extends Fragment {
public static final String TAG = TestFragment.class.
getSimpleName();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_test, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
NavController navController = Navigation.findNavController(view);
Button btnJump = view.findViewById(R.id.btn_jump);
btnJump.setOnClickListener(v ->
{
navController.navigate(R.id.action_testFragment_to_test2Activity);
});
}
}
3、Navigation Graph
  • test_graph_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
    <navigation 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:id="@+id/test_graph_navigation"
    app:startDestination="@id/testFragment">
  <fragment
    android:id="@+id/testFragment"
    android:name="com.my.navigation.fragment.TestFragment"
    android:label="fragment_test"
    tools:layout="@layout/fragment_test">
  <action
    android:id="@+id/action_testFragment_to_test2Activity"
    app:destination="@id/test2Activity" />
</fragment>
<activity
  android:id="@+id/test2Activity"
  android:name="com.my.navigation.Test2Activity"
  android:label="Test2Activity" />
</navigation>
4、Activity Layout
  • activity_test1.xml
<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Test1Activity">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test1Activity"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
  <fragment
    android:id="@+id/nhf"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:navGraph="@navigation/test_graph_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • activity_test2.xml
<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Test1Activity">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test2Activity"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
  <Button
    android:id="@+id/btn_back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="返回"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
5、Activity Code
  • Test1Activity.java
public class Test1Activity
extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_test1);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) ->
{
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
}
  • Test2Activity.java
public class Test2Activity
extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_test2);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) ->
{
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
Button btnBack = findViewById(R.id.btn_back);
btnBack.setOnClickListener(v ->
{
finish();
});
}
}
posted @ 2025-09-20 09:20  yxysuanfa  阅读(10)  评论(0)    收藏  举报