Skip to content

Instantly share code, notes, and snippets.

@rosuH
Created November 9, 2017 07:55
Show Gist options
  • Save rosuH/15261a793ef08a751e1a4597f4929353 to your computer and use it in GitHub Desktop.
Save rosuH/15261a793ef08a751e1a4597f4929353 to your computer and use it in GitHub Desktop.
Encapsulate implementation details in a static method to optimize intent communication
/**
* 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