preface

When I looked at the View source code, I saw a lot of hexadecimal constants and bit operations.

/ / the class source code
static final int PFLAG3_VIEW_IS_ANIMATING_TRANSFORM = 0x1;
static final int PFLAG3_VIEW_IS_ANIMATING_ALPHA = 0x2;
static final int PFLAG3_IS_LAID_OUT = 0x4;
static final int PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT = 0x8;
static final int PFLAG3_CALLED_SUPER = 0x10;
static final int PFLAG3_APPLYING_INSETS = 0x20;
static final int PFLAG3_FITTING_SYSTEM_WINDOWS = 0x40;
static final int PFLAG3_NESTED_SCROLLING_ENABLED = 0x80;
static final int PFLAG3_SCROLL_INDICATOR_TOP = 0x0100; .// intent
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)
Copy the code

But I never thought about why I wrote this. Just recently, I found an Article on Android state management best practices, even if you don’t have to go to Mars to plant potatoes! Android uses hexadecimal to manage multiple states.

thinking

Q: How did I deal with the multi-state situation? A: through string concatenation; Use containers such as groups, lists, or maps; Q: How does hexadecimal manage multiple states? What are its advantages? A: The arrangement is as follows!

Base 16 and base 2

Theoretical basis

  • Master the conversion between hexadecimal and binary, each hexadecimal corresponds to 4 bits of binary.
  • Master bit operation
    • And:0x6 & 0x4 ==> 0110 & 0100 = 0100
    • Or:0x2 | 0x4 ==> 0010 | 0100 = 0110
    • Take the:~ 0x2 ==> ~ 0010 = 1101

We practice

/** * For example, there are many states of weather: * snow * rain */
const val snow  = 0x1  / / 0001
const val rain  = 0x2  / / 0010
const val sun   = 0x4  / / 0100
const val cloud = 0x8  / / 1000./ / the weather is sleet, can be expressed as: snow | rain
val weather = snow or rain  
/ / 0011 = 0001 | 0010.// After a while, it stopped raining: snow & ~rain
weather = weather and rain.inv()
// 0001 = 0011&~ 0010 = 0011&1101.// Check whether it is raining (weather & rain)
val isSnow = weather and rain
// 0000 = 0001&0010
Copy the code

Pay attention to the notes for better understanding

Usage and Advantages

Summary of usage:

  • When you need toIncrease stateusingBitwise or
  • When you need toRemove the stateusingAfter taking the inverse, press bit and
  • When you need toJudge stateusingAccording to bit and, judge whether the result is 0, 0 does not exist, and vice versa
  • A top priority

Compared with the original string concatenation or collection method, the advantages are particularly obvious:

  • States can be added or removed more easily without string splitting or collection addition or removal
  • More convenient to determine the state, no need to set traversal
  • State management can be done using only ints, which are lighter for storage

A top priority

Hexadecimal multistate management is essentially binary management, that is, the number of bits’ 1 ‘is in.

  • Each state can only be individually bit'1', as shown in the figure below

  • If you want to do this, you can verify that by setting a different value, you know that you can only use the values defined above.

    const val snow = 0x3  / / 0011
    const val rain = 0x2  / / 0010./ / the weather is sleet, can be expressed as: snow | rain
    val weather = snow or rain  
    / / 0011 = 0011 | 0010
    
    // It can be seen that the snow state is set to 0x3. When the state is increased, or after the operation, it is not enough to represent the sleet state.
    Copy the code
  • Since the nature is binary, it should be possible to use other bases such as decimal. But the reason I chose hexadecimal is because it’s easier to convert, it’s more intuitive.

reference

Even if you don’t have to plant potatoes on Mars, be sure to learn Android state management best practices!