Serialize the Intent and pass it like a Uri!!!

Serialize the Intent and pass it like a Uri!!!

[[207121]]

1. Do you really need to create a new Intent?

In Android, how many ways are there to open an Activity? However, no matter which method you use, you can't escape creating an Intent and then startActivity().

So, what if you want to determine the page to jump to based on data?

What should be done better? DeepLink seems to be a good solution. In AndroidManifest.xml, define the data field, mark the scheme, host, etc., and then pass it according to the rules, so that you can jump to certain pages.

But is this really convenient?

Do we need to set the data on each Activity that needs to be redirected? Of course, there are some mature Deeplink solutions on Github. You only need to set the data for a certain Activity, and then all deeplink links will jump to this Activity. Finally, the RouterActivity will decide where to jump and carry the parameters.

So is there any other solution?

2. toUri() of Intent

Until I found that Intent actually has a toUri() method, I thought there was a way to simply serialize Intent.

As can be seen from the document, the toUri() method can convert an Intent into a URI, which contains some necessary parameters such as action, categories, flags, etc.

So what is the URI that startActivity at the beginning of the article finally converted into?

If you look closely, the data of flag, composite, and putExtra are all serialized into a string.

How to use the obtained Uri? You can use a static method of Intent, parseUri(), to convert an Intent URI into an actual Intent object.

In this case, the following method will also call TwoActivity and bring over a balabala ID data.

At this point, we have basically explained what this article needs to explain. However, as aspiring programmers, we still need to dig deeper to find out what toUri() actually does.

3. What does toUri() do?

Let's take a look at the specific implementation of toUri().

From the source code implementation, toUri() just reads each field and then serializes it according to the rules, and parseUri() just deserializes it according to the rules. If you are skilled, you can basically write the URI of the Intent without using the toUri() method conversion.

4. Are there any hidden dangers?

The APIs we use are all officially provided for Intent, and there seems to be no problem in using them. But is it really as good as it seems?

From the perspective of parameter passing, the toUriInner() method is the method in the toUri() method that serializes the passed data. Let's take a look at the specific implementation.

As you can see, the toUriInner() method converts basic data types to corresponding types. For example, S.id=balabala means a String value balabala with the key id.

It seems that all types of parameter passing have been covered, but it is not so beautiful. Did you notice that there is no parameter passing about Bundle? Did I miss it?

I won't even look at the code, let's do an experiment to verify it.

Look at the Log output:

As you can see, the toUri() method does not do any special processing for the serialization of the Bundle parameter.

The conclusion is that although the toUri() and parseUri() methods are indeed very useful, they are also flawed. The data passed by the Bundle cannot be serialized into a Uri.

In actual use, we need to have strict requirements on the parameters passed and avoid using Bundle to pass data. Of course, we can also implement the logic of serializing and deserializing Bundle ourselves.

V. Conclusion

In this way, you can simply pass the Intent and get an IntentUri from the backend server, so you don't need to set a specific page for each click. Of course, how to use it depends on the actual usage scenario.

[This article is an original article by 51CTO columnist "Zhang Yang". Please contact the author through WeChat official account to obtain authorization for reprinting]

Click here to read more articles by this author

<<:  Front-end: 6 common HTML5 misuses

>>:  6 good ways to clean up mobile phone garbage, 99% of people don’t know!

Recommend

Artificial beef jerky, do you dare to try it?

Produced by: Science Popularization China Author:...

How to operate a good community? Share 4 community skills!

Since 2017, communities have been pushed to the f...

Useful Information | General Process for Online Event Operation and Promotion

There is no clear definition of operation and spe...

Indians who don't have TikTok have found more than 100 alternatives to TikTok

[[348657]] India, a country full of miracles, is ...

In 2019, there are two modes of content operation!

All content production is the product of certain ...

With a corset, I will have a “slim waist”?

The fairies are very open about their love for be...