Hello! The new keyword creates a new instance in the heap each time, right? Hence, the question arose: are resources being expended under such a scheme for invoking a new Activity as in the example on the site ?

 public void sendMessage(View view) { Intent intent = new Intent(this, DisplayMessageActivity.class); startActivity(intent); } 

those. do we create a new instance instance every time? And what will happen to the previously created by the previous click? Will the garbage collector take it? Or maybe it will not be re-created, but the previously created one will be used?

Wouldn't it be more appropriate to do this:

 public class MainActivity extends AppCompatActivity { private Intent intent; ... @Override protected void onCreate(Bundle savedInstanceState) { ... intent = new Intent(this, SettingsActivity.class); ... } ... public void btnClick(View view) { startActivity(intent); } ... } 

those. create an object once in the onCreate event, and then call it when the button is pressed (for example)

Please help me figure out, or send me to learn the basics of creating instances of classes :)

  • one
    Yes, the second option is correct. Tutorials are written to demonstrate some specific features, they do not take into account optimization and other such things, because then it will not be a compact code sample on a specific issue, but a full-fledged project, which is too cumbersome to publish for each local example. The correct architecture of the entire application is studied in a completely different tutorial - pavlofff
  • @pavlofff Please post your comment as a response. - Nicolas Chabanovsky

3 answers 3

I see no point in doing this in your case. You make the object a class field, or you'll create it locally, which does not affect the performance and memory costs, it is unlikely that the user will notice a delay of several milliseconds, or even less, that will be spent on creating the object. So you just inflate the code with unnecessary variables. Better create an intent through the factory method of the activation you want to run.

 public class SecondActivity extend Activity { public static createIntent(Context context) { return new Intent(context, SecondActivity.class); } } private void someMetod(){ startActivity(SecondActivity.createIntent(context)); } 

This will give you an advantage when you want to transfer the parameters to a new activation, because the parameters set in the intent will be in one place, which will shorten the time for changing the code.

    There is an object pool design pattern. It is useful when creating and / or destroying an object takes a long time, objects are created often, but few of them exist at the same time.

    In your case, when there is only one object, it all comes down to storing a single instance. Going to teach can be useful design patterns, in particular, generating patterns .

      start drilling from here , and there is also a lot of interesting information for you. In general, learn more flag intent .

      For example, FLAG_ACTIVITY_CLEAR_TOP - searches for a created Activity in the taskbar. If it finds it, it opens it, and everything above it closes it.