当前位置: 首页 > news >正文

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

一、使用 startActivity 跳转

1、Fragment Layout
<?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"><TextViewandroid: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" /><Buttonandroid: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"><TextViewandroid: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" /><fragmentandroid: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"><TextViewandroid: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" /><Buttonandroid: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
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
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
<?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"><TextViewandroid: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" /><Buttonandroid: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"><fragmentandroid:id="@+id/testFragment"android:name="com.my.navigation.fragment.TestFragment"android:label="fragment_test"tools:layout="@layout/fragment_test"><actionandroid:id="@+id/action_testFragment_to_test2Activity"app:destination="@id/test2Activity" />
</fragment>
<activityandroid: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"><TextViewandroid: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" /><fragmentandroid: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"><TextViewandroid: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" /><Buttonandroid: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();
});
}
}
http://www.gsyq.cn/news/8380.html

相关文章:

  • 强化学习之父 Richard Sutton: 如今AI正进入“经验时代” - 指南
  • 嵌入式笔记系列——UART:TTL-UART、RS-232、RS-422、RS-485 - 指南
  • 实用指南:【保姆级教程】TEXTurePaper运行环境搭建与Stable Diffusion模型本地化
  • 高级数据结构手册
  • 【无人艇协同】基于matlab面向海事安全的双体无人艇分布式协同任务规划(目标函数:总时间满意度)【含Matlab源码 14161期】博士论文 - 教程
  • 深入解析:【Fiora深度解析】手把手教你用固定公网IP搭建专属聊天系统!
  • 使用JavaScript和CSS创建动态高亮导航栏
  • wxt 开发浏览器插件的框架
  • Gridspech 全通关
  • 纯国产GPU性能对比,谁才是国产算力之王?
  • 英伟达入股英特尔,当竞争对手便成协作者,真正受益的......
  • ODT/珂朵莉树 入门
  • 绯闻女孩不只会八卦:从“验明正身”到“抓内鬼”,Gossip的进阶玩法
  • reLeetCode 热题 100- 15. 三数之和 - MKT
  • US$94 T300 Key Programmer Spanish Blue 2016 V16.8 Full
  • US$99 VVDI MB NEC Key Adaptor
  • testuserpython
  • [Nacos/Docker/MCP] Nacos 3.x : 为 AI MCP 而生
  • AIGC拾遗:Flash Attention
  • Python-CSV库
  • C++小白修仙记_LeetCode刷题_双指针
  • 前路漫漫亦灿灿 往事堪堪亦澜澜
  • 现代汽车前瞻杯2025牛客暑期多校训练营3
  • 详细介绍:[新启航]白光干涉仪在微透镜阵列微观 3D 轮廓测量中的应用解析
  • 2023 CCPC 深圳 F
  • 完整教程:【算法】双指针(三)[快慢指针]-快乐数
  • 9.19做题资料:哈希表查找时间复杂度分析
  • 实用指南:容器逃逸漏洞
  • 深入解析:卷对卷(Roll-to-Roll,R2R)技术的应用领域和技术进展
  • 三种方式处理SpringBoot全局异常