First give a welfare, attention after the public number back to “yellow car”, you can get a good samaritans prepared by the “yellow car” free monthly card.

This is the second in the interview series we agreed on. This week we will continue to look at some of the must-have interview questions.

Interview: Tell me about the four startup modes for Android

Talk about the Activity lifecycle

When I tell you interviewers will ask this question, you must slap me in the face. Such a problem on the net search a lot of, casually recite can easily face, it is necessary so much trouble to listen to the “south dust” blow a wave?

So you start reciting.

  • Start Activiy: onCreate => onStart() => onResume() and the Activity enters the running state.

  • The Activity exits the background (Home or starts a new Activity): onPause() => onStop().

  • The Activity returns to the foreground: onRestart() => onStart() => onResume().

  • If the Activity is running out of memory during the background, the startup process is restarted when it is started again.

  • Lock screen: onPause() => onStop().

  • Unlock: onStart() => onResume().

Let’s just do a picture, just to make it clearer.

It’s pretty clear at first glance. Yeah, it’s nice to be able to memorize it, even memorize it in your head.

But but!!

Would an interviewer still ask you that?

Will!

For entry-level or even entry-level Android developers?

No pun intended, but how do you ask?

Before I ask how, I want to break down each of these methods to help you understand. Notice what I put in bold.

  • OnCreate () For each activity we override this method, which is called when the activity is first created. This is where you should initialize activities, such as loading layouts, binding events, and so on.

  • The onStart() method is called when the activity changes from invisible to visible.

  • The onResume() method is called when the activity is ready to interact with the user. The activity must be at the top of the return stack and running.

  • OnPause () is called when the system is about to start or resume another activity. We usually use this method to free up some CPU intensive resources (such as displaying maps or large graphics) and save some critical data (such as user input, etc.), but this method must be executed fast or it will affect the use of new top-stack activities.

  • The onStop() method is called when the activity is completely invisible. The main difference between onPause() and onPause() is that if the new activity is a dialog activity, onPause() is executed, while onStop() is not.

  • The onDestroy() method is called before the activity is destroyed, after which its state changes to destroyed.

  • OnRestart () is called before the activity changes from a stopped state to a running state, that is, the activity is restarted.

Interested in the official code can go to read the source code, which Activity single onXXX callback method API is up to more than 80 kinds, simple memory down is useless. Well, it might work. You can be proud of your super memory!

The above methods, with the exception of onRestart(), are all opposed to each other and can be divided into three phases.

  • The full lifetime activity, between the onCreate() and onDestroy() methods, is the full lifetime. Typically, an activity does various initializations in onCreate() and frees memory in onDestroy().

  • Visible survival period is highlighted!! I met this question in the interview, actually I knew this question, but I misunderstood the interviewer’s meaning and answered the question differently. What the activity goes through between the onStart() and onStop() methods is the visible lifetime. During the visible lifetime, an activity is always visible to the user, even if it may not interact with the user. We can use these two methods to manage resources that are visible to the user. For example, load resources in the onStart() method and release resources in the onStop() method to ensure that stopped activities do not take up too much memory.

  • The foreground lifetime activity experiences between onResume() and onPause() is the foreground lifetime. During the life of the front desk, activities are always in the running state. At this time, activities can interact with users. We usually see and contact the most activities in this state.

How exactly?

With all that said, we can simulate several interview scenarios.

1. Suppose there is a requirement in your project to register a broadcast when the specified Activity is visible to the user, and unregister the broadcast when the user is not, in which two callbacks should this be done?

2. If some data needs to be saved to the database while an Activity is jumping (or leaving), do you think it’s better to do it in onPause() or onStop()?

3, Activity A starts Activity B. Briefly describe the changes in their respective life cycles.

4. Does Activity A call onStop() when Activity B is visible?

First of all, the first question, we carefully looked at the above explanation of children’s shoes are certainly more clear. After an Activity is started, the onCreat() method is called to lay out and close the event. The Activity is not visible until the onStart() method is called back. So just answer onStart() and onStop().

Second, as anyone familiar with the Activity lifecycle knows, onPause() is much easier to trigger than onStop(). OnPause () => onStop() in most cases, but it’s hard to guarantee that the onStop() method will work properly every time. For example, the onStop() system is recycled before it can be run.

It is important to note that this operation should be as fast as possible, otherwise it will definitely affect the life cycle of the next Activity.

As long as Activity B at the top of the stack is visible, Activity A at the bottom of the stack must call onStop().

If you were asked the same way as the fourth question, you would assume it would be called, but you would also be asked to doubt your life. But since you think you must call onStop() and you can’t find a way not to, you answer bluntly that you will.

As mentioned above, the Activity calls onStop() when the Activity is completely invisible, so we just need to figure out how to specify the visible state.

First, an Activity pops up a Dialog and only calls onPause() during its lifecycle, so let’s assume that Activity B pops up as a Dialog, and even if Activity B pops up as a normal Activity, we set the page of Activity B to a lower opacity. In fact, the same effect, do not believe you try to print a log.

summary

It may seem like we don’t pay much attention to the Activity life cycle, because we feel like everything is taken for granted, but success lies in every detail, and using the Activity life cycle properly will make your application more smooth.

Well, it’s almost midnight in a twinkling of an eye, and the work of the author has been busy lately. But still want to non-stop loaded force, you see, “StormZhang” rely on loaded force to help the company financing XXX million, that loaded force is effective.


Welcome to follow nanchen’s official account: Nanchen if you like, you can choose to share with everyone. If you have a good article, welcome to contribute, praise all belong to you.

Long press the qr code above to pay attention

Do not finish the open source, write not finish hypocritical

Let’s take a look at Nanchen’s growth notes