Story telling activity life cycle (cat’s life)

Hello everyone, most online articles about the life cycle of activity in Android are basically direct mapping and translation APIs, which are relatively general and vague.

Let me tell you a story:

A man named user, Ta has several cats, and a cat named activity. User witnessed the life of activity cat.

The user wants a kitten (wants to use some functions), so he lets a female cat mate with a male cat (touch the screen to open the activity interface),

After a period of activity launched, the kitten was born (oncreate), named activity, and kept in a cage (APP).

The owner took it out, and the kitten activity woke up (OnStart), opened its eyes (onresume), turned its head, stretched out its claws, and cried for something to eat... (activity running).

Then the host feeds and plays with it (user's operation on activity)

The user thought he had enough time with the activity, so he (opened the new activity) put it in a cage and played with other activity comes into the forest.

Activity starts to onpause

After a while, the owner opened the cage and called its name (user returns to the activity). When it heard it, it immediately opened its eyes (onresume) and ran to play with the owner (activity running)

Later, the owner locked it up and left. The activity continued to onpause

Then the owner turned off the light (the activity is no longer visible), and the activity fell asleep when it felt dark (onstop)

I don't know when the owner came back, knocking on the cage and calling the name of activity (user navigates to the activity),

The activity is pulled back to the real world from the dream, restored to the state (onrestart), and then woke up (OnStart) and opened his eyes (onresume)

After a long time

Once, the owner (pressing the home page, multitasking or power button) went away and didn't come back for a long time. The food at home was not enough and was soon eaten up by everyone (apps with higher priority need memory).

Because the activity is very weak (low priority) and locked in a cage (not bound to the foreground service), it eats very little food.

After several meals of hunger and thirst, everyone fainted and went into shock (APP process killed).

When the owner came back, he saw the poor activity lying motionless and couldn't wake up, so he hurried to the veterinary hospital for rescue (user navigates to the activity).

The doctor gave it blood and nutrition (the system redistributed it to memory). After a period of time, the activity finally came back to life. It felt as if it had come out of its mother's stomach again (oncreate).

It wakes up (OnStart), opens its eyes (onresume), and fights with its owner (activity running). After a while, the owner takes it home

As the saying goes, if you don't die, you must have a blessing. After that, the owner does take care of the activity.

But one day, the activity was destroyed by the system.

The activity is dead (ondestroy), and the user is very sad to bury it (activity shutdown)

User missed his time with activity and was unwilling, so he asked for a new kitten, whose name is still activity

Activity grows up day by day. Its owner dotes on it very much, but it is becoming more and more rebellious.

Finally, one day, the owner was fed up with it. He felt that the activity was too hateful (or useless), so he (click the back button) abandoned it (the activity is finished)

The activity does not adapt to the harsh external environment and soon dies (ondestroy). Nature buries it with wind and soil (activity shutdown)

This is the life of activity cat.

Zblibrary provides methods such as isalive() and isrunning() to judge the life cycle status of activity and fragment;

And the runuitthread (runnable runnable) method to avoid running crash after ondestroy caused by direct runonitthread (runnable runnable);

There are also thread management classes threadmanager. In baseactivity and basefragment, you can easily create new threads by runthread (string tag, runnable, runnable),

It is automatically released after ondestroy. There is no need to worry about running new thread (runnable). Start() directly after ondestroy, wasting resources, or even using recycled variables to cause crashes.

Zblibrary Android rapid development framework

Download trial

ZBLibraryDemoApp.apk

Download address (if you think it's good, just support star ^ ^)

https://github.com/TommyLemon/Android-ZBLibrary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>