Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android intent setFlags issue

I came across some code for an intent that launches a new activity that contains an intent with an int directly: intent.setFlags(268435456);

instead of the appropriate Intent.FLAG_ACTIVITY_NEW_TASK or what have ya.

What's the best way to figure out what this int actually equates to such that we don't break the app by setting the wrong one? I'd like to clean this code up a bit.

like image 271
Tom Hammond Avatar asked Oct 29 '25 00:10

Tom Hammond


1 Answers

Flags are based on setting individual bits, so the first thing you want to do is convert that value to binary. You can do this using any standard calculator app that allows you to convert between decimal and binary. In this case:

‭0001 0000 0000 0000 0000 0000 0000 0000‬
   ^

Only one bit is set to true (28th bit from the right). All of the Intent flags are declared as hex values (e.g. FLAG_ACTIVITY_NEW_TASK = 0x10000000) so then you probably just want to convert this to hex and figure out which one matches up.

public static final int FLAG_ACTIVITY_NEW_TASK = 0x10000000;

And it turns out that's the one we were looking for in this case. Let's say you have one that's more complicated -- a combination of multiple flags:

intent.setFlags(335544320);

One again, convert to binary:

‭0001 0100 0000 0000 0000 0000 0000 0000‬
   ^  ^

So we have two flags here. Split these into two separate values, with only one flag set for each:

0001 0000 0000 0000 0000 0000 0000 0000
0000 0100 0000 0000 0000 0000 0000 0000

Convert to hex:

0x10000000
0x04000000

Looking in the source for Intent, this maps to:

Intent.FLAG_ACTIVITY_NEW_TASK
Intent.FLAG_ACTIVITY_CLEAR_TOP

So your equivalent replacement would be:

intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP);
like image 53
Kevin Coppock Avatar answered Oct 30 '25 15:10

Kevin Coppock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!