3.3 Start an external Activity

0. Introduction

What you’ll learn

How to use the Bundle class to pass arguments from one Fragment to another
How to use the Safe Args Gradle plugin for type safety
How to add a “share” menu item to an app
What an implicit intent is and how to create one

What you’ll do

Modify the AndroidTrivia code to use the Safe Args plugin, which generates NavDirection classes.
Use one of the generated NavDirection classes to pass type-safe arguments between the GameFragment and the game-state fragments.
Add a “share” menu item to the app.
Create an implicit intent that launches a chooser that the user can use to share a message about their game results.

2. Set up and use the Safe Args plugin

Before users can share their game results from within the AndroidTrivia app, your code needs to pass parameters from one Fragment to another. To prevent bugs in these transactions and make them type-safe, you use a Gradle plugin called Safe Args. The plugin generates NavDirection classes, and you add these classes to your code.

2.1 Why Safe Args plugin

Your app could use a Bundle to pass data from Fragment A to Fragment B. For example, Fragment A creates a bundle and saves the information as key-value pairs, then passes the Bundle to Fragment B. Then Fragment B uses a key to fetch a key-value pair from the Bundle. This technique works, but it can result in code that compiles, but then has the potential to cause errors when the app runs.

The kinds of errors that can occur are:

  • Type mismatch errors. For example, if Fragment A sends a string but Fragment B requests an integer from the bundle, the request returns the default value of zero. Since zero is a valid value, this kind of type mismatch problem does not throw an error when the app is compiled. However, when the user runs the app, the error might make the app misbehave or crash.
  • Missing key errors. If Fragment B requests an argument that isn’t set in the bundle, the operation returns null. Again, this doesn’t throw an error when the app is compiled but could cause severe problems when the user runs the app.

To help with these problems, Android’s Navigation Architecture Component includes a feature called Safe Args. Safe Args is a Gradle plugin that generates code and classes that help detect errors at compile-time that might not otherwise be surfaced until the app runs.

2.2 Add Safe Args to the project

In Android Studio, open the project-level build.gradle file.
Add the navigation-safe-args-gradle-plugin dependency, as shown below:

// Adding the safe-args dependency to the project Gradle file
dependencies {
   ...
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigationVersion"

}

Open the app-level build.gradle file.
At the top of the file, after all the other plugins, add the apply plugin statement with the androidx.navigation.safeargs plugin:

// Adding the apply plugin statement for safeargs
apply plugin: 'androidx.navigation.safeargs'

Re-build the project. If you are prompted to install additional build tools, install them.

The Safe Args plugin generates a NavDirection class for each Fragment. These classes represent navigation from all the app’s actions.

For example, GameFragment now has a generated GameFragmentDirections class. You use the GameFragmentDirections class to pass type-safe arguments between the game Fragment and other fragments in the app.

2.3 Add a NavDirection class to the game Fragment

// We've won!  Navigate to the gameWonFragment.
// view.findNavController().navigate(R.id.action_gameFragment_to_gameWonFragment)
// Using directions to navigate to the GameWonFragment
view.findNavController()
    .navigate(GameFragmentDirections.actionGameFragmentToGameWonFragment())

// Game over! A wrong answer sends us to the gameOverFragment.
// view.findNavController().navigate(R.id.action_gameFragment_to_gameOverFragment)
// Using directions to navigate to the GameOverFragment
view.findNavController()
	.navigate(GameFragmentDirections.actionGameFragmentToGameOverFragment())

3. Add and pass arguments

In this task, you add type-safe arguments to the gameWonFragment and pass the arguments safely into a GameFragmentDirections method. Similarly you then will replace the other Fragment classes with their equivalent NavDirection classes.

Step 1: Add arguments to the game-won Fragment

Step 2: Pass the arguments

Step 3: Replace Fragment classes with NavDirection classes

4. Sharing game results

Implicit intents

An Intent is a simple message object that’s used to communicate between Android components. There are two types of intents: explicit and implicit. You can send a message to a specific target using an explicit intent. With an implicit intent, you initiate an Activity without knowing which app or Activity will handle the task. For example, if you want your app to take a photo, you typically don’t care which app or Activity performs the task. When multiple Android apps can handle the same implicit intent, Android shows the user a chooser, so that the user can select an app to handle the request.

Each implicit intent must have an ACTION that describes the type of thing that is to be done. Common actions, such as ACTION_VIEW, ACTION_EDIT, and ACTION_DIAL, are defined in the Intent class.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值