Reputation: 982
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Intent newIntent = new Intent(context, SpeedDialerActivity.class);
intent = new Intent(context, SpeedDialerActivity.class);
context.startActivity(newIntent||intent(received from argument)); // which object is better here.
}
}
Is it better to use the intent received in the argument of the onReceive method or is it better to create and use a new object of type Intent class. If I use the Intent object received as argument in onReceive method would the behavior be ambiguous.
Upvotes: 1
Views: 81
Reputation: 16780
A basic rule of good programming in Java (and any language) is not to reuse variables for different purposes. The safest way to prevent issues is to declare input arguments as final
. That way you wouldn't even be able to do such value change. Do it like this:
public void onReceive(final Context context, final Intent intent) {
Any way, having a second variable will not cause any significant memory changes, if any, but your code will be much safer. You could save some memory if the input intent
object was reused as is, but don't do that either! You should think about these kind of memory optimizations only if you see it is an issue. And even in that case there is usually other ways to solve the problems.
It highly depends on your application what is the most memory intensive but in practice resources, like images take up most of the memory. You should use the already available caching mechanisms for that. Just make a google search, zillions of articles and Google IO videos are available. Some examples:
Upvotes: 1