简体   繁体   中英

Static vs non static Activity intent calling method

Here I am a little bit confuse about Static Intent calling method vs Direct intent calling. What is better choice for creating New Activity as memory point of view?

As per I know if intent method calling with static it contains memory for application lives. is it true or not?

Let move to take example :

In Activity B

public static Intent newIntent(Context context) {
       Intent intent = new Intent(context, B.class);
       return intent;
   }

Calling Activity B from Activity A

In Activity A

 startActivity(B.newIntent(this));

or

In other way direct calling activity can not live after finish() Calling activity. right?

startActivity(new Intent(context, B.class));

Still I guess second is better then as code point of you and memory point of view. But I see many projects contains first(static calling) method. So I want to know what is better selection to calling new Activity?

The method public static Intent newIntent() is static, but that's all that is static. The use of this static method is that you can call B.newIntent() without having an instance of B .

The context you pass to B.newIntent(this) is not static and therefore it does not matter if you create the intent in A or in B .

This in A

startActivity(B.newIntent(this));

is no different than this in A

startActivity(newIntent(this));

private Intent newIntent(Context context) {
   Intent intent = new Intent(context, B.class);
   return intent;
}

So I want to know what is better selection to calling new Activity?

In functionality it makes no difference. If there is any difference in memory usage, it will be so little you won't notice it.

As far as coding style goes, it would be better to keep the creation of the intent it in A , because A is starting B , and B should not care how A takes care of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM