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

Damage to the liver, change genes? Do you still dare to "eat rainbow"?

Snacks are really a magical thing. From children ...

The big-screen phone bet is right! iOS has reversed its global market share

[[125989]] Last year, Apple released two large-sc...

What's wrong with Google's new icon?

Some time ago, Google released a new logo, which ...

No kidding, Corgis are really cattle dogs!

Corgis are petite and cute, and when you see them...

People in this group should be careful when getting the new coronavirus vaccine!

Spring is the peak season for pollen allergies Ca...

iOS 16.6 update released, a retirement version?

iOS 16.6 Beta 2 Update This morning, Apple pushed...

Tips for the 2nd phase of the Chan Theory Basic Course in 2022

Tips for the 2nd 2022 resource introduction of th...

There is a long white thread coming out of my eye. What is it?

Source: Dr. Curious The cover image and the images...

Get pregnant as early as possible! HPV vaccines are free in many places...

The 2022 Government Work Report pointed out that ...

Practical tips: How to promote an event well?

First, let me give you a few tips: Tip 1: How to ...