Created
November 9, 2017 07:55
-
-
Save rosuH/15261a793ef08a751e1a4597f4929353 to your computer and use it in GitHub Desktop.
Encapsulate implementation details in a static method to optimize intent communication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* public name: SendActivity | |
* comment: start activity(using intent) by calling static final method instead of creating one by itself. | |
*/ | |
public class SendActivity extends AppCompatActivity{ | |
... | |
//Start Receiver Activity | |
boolean exampleKey = someFunToGetExampleKey(); | |
Intent intent = ReceiveActivity.newIntent(SendActivity.this, exampleKey); | |
startActivity(intent); | |
} | |
/** | |
* public name: ReceiveActivity | |
* commnet: providing a sitaic final method that can be called to construct a intent | |
*/ | |
public class ReceiveActivity extends AppCompatActivity { | |
private static final String EXTRA_EXAMPLE_KEY = "com.example.optimizeIntent.example_key" | |
public static Intent newIntent(Context packageContext, boolean exampleKey) { | |
Intent intent = new Intent(packageContext, ReceiveActivity.class); | |
intent.putExtra(EXTRA_EXAMPLE_KEY, exampleKey); | |
return intent; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment