目录
Android Jetpack应用指南学习笔记11使用safe args方式安全传递参数
1.使用safe args之前我们先要安装插件,步骤如下:
1.1.在Project的build.gradle目录下添加插件:
1.2.在app的build.gradle目录下添加如下配置:
2.通常Fragment之间我们传递参数是这样的:
3.通过safe args安全方式传递参数:
4.普通方式接收传递过来的参数:
5.通过safe args接收参数:
6.主界面MainFragment完整代码如下:
7.第2个界面SecondFragment完整代码如下:
9.引入插件,在xml添加name和age2个属性后,重新进行项目同步,同步成功后会出现如下文件:编辑
10.运行结果如下图所示:
10.1 通过普通方式传递参数获取到的结果为:
10.2 通过safe args传递参数获取到的结果为:
11.总结:
12.项目源码地址如下:
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0"
buildscript { repositories { google() jcenter() } dependencies { classpath "com.android.tools.build:gradle:4.0.0" classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }
apply plugin: 'androidx.navigation.safeargs'
Bundle bundle = new Bundle(); bundle.putString("name", "Michael"); bundle.putInt("age", 30); Navigation.findNavController(v).navigate(R.id.action_mainFragment_to_secondFragment, bundle);
Bundle bundle = new MainFragmentArgs.Builder().setName("小霍").setAge(18).build().toBundle(); Navigation.findNavController(v).navigate(R.id.action_mainFragment_to_secondFragment, bundle);
//普通方式接收参数 if(bundle != null){ String userName = bundle.getString("name"); int age = bundle.getInt("age"); Log.d("bundleData1", "姓名:" + userName + "年龄:" + age); tvTitle.setText(userName); tvAge.setText(String.valueOf(age)); }
if (bundle != null) { userName = MainFragmentArgs.fromBundle(bundle).getName(); age = MainFragmentArgs.fromBundle(bundle).getAge(); Log.d("bundleData2", " 姓名: " + userName + " 年龄: " + age); tvTitle.setText(userName); tvAge.setText(String.valueOf(age)); }
/** * @author: njb * @date: 2020/9/5 0005 1:20 * @desc: */ public class MainFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_main, container, false); //方法一:未采用safe args的传递方式 view.findViewById(R.id.btnToSecondFragment).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bundle bundle = new Bundle(); bundle.putString("name", "Michael"); bundle.putInt("age", 30); Navigation.findNavController(v).navigate(R.id.action_mainFragment_to_secondFragment, bundle); } }); //方法二://通过safe args完成参数传递 /* view.findViewById(R.id.btnToSecondFragment).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bundle bundle = new MainFragmentArgs.Builder().setName("小霍").setAge(18).build().toBundle(); Navigation.findNavController(v).navigate(R.id.action_mainFragment_to_secondFragment, bundle); } });*/ view.findViewById(R.id.btnToMineFragment).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Navigation.findNavController(v).navigate(R.id.action_mainFragment_to_mineFragment); } }); return view; } }
package com.example.navigationbasicdemo.fragment; import android.os.Bundle; import android.util.Log; 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; import com.example.navigationbasicdemo.R; /** * @author: njb * @date: 2020/9/5 0005 1:23 * @desc: */ public class SecondFragment extends Fragment { TextView tvTitle, tvAge; @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_second, container, false); tvTitle = view.findViewById(R.id.tvTitle); tvAge = view.findViewById(R.id.tvSub); Bundle bundle = getArguments(); //普通方式接收参数 if(bundle != null){ String userName = bundle.getString("name"); int age = bundle.getInt("age"); Log.d("bundleData1", "姓名:" + userName + "年龄:" + age); tvTitle.setText(userName); tvAge.setText(String.valueOf(age)); } //通过safe args接收参数 /* if (bundle != null) { String userName = MainFragmentArgs.fromBundle(bundle).getName(); int age = MainFragmentArgs.fromBundle(bundle).getAge(); Log.d("bundleData2", " 姓名: " + userName + " 年龄: " + age); tvTitle.setText(userName); tvAge.setText(String.valueOf(age)); }*/ return view; } }
以上就是今天的博客内容,使用safe args方式如果不配置navigation布局文件属性传递参数和获取参数都会失败,具体的可以参考官方文档,普通的方式和之前一样使用Bundle传递参数,需要注意的是接收参数时要判断bundle是否为空!!!如有问题及时沟通,希望对各位有所帮助,一起学习成长!!
NavigationDemo: Navigation(一)基础入门,包含布局创建添加和使用