Activity Lifecycle callbacks methods

来源:Android development tutorial series 2018第40集
原作者:PRABEESH R K

1.onCreate()

1️⃣You must implement this method, which fires when the system first creates the activity.
2️⃣In the onCreate() method, you can perform basic application startup logic that should happen only once for the entire life of the activity.
3️⃣This method receives the parameter savedInstanceState, which is a Bundle object containing the activity's previously saved state.

1.这是一个必须实现的方法,它会在系统第一次创建这个活动时被调用。

2.你可以在这个方法中写一些在这个活动生命周期中只会发生一次的启动逻辑。

3.这个方法会接收到一个包含活动预先已保存状态的Bundle对象。

2.onStart():

1️⃣After the onCreate() method finishes execution, the activity enters the Started state.
2️⃣When the activity enters the Started state, the system invokes onStart() callback.
3️⃣The onStart() call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive.
4️⃣For example, this method is where the app initializes the code that maintains the UI.
5️⃣Within this method you can register a BroadcastReceiver that monitors changes that are reflected in the UI.

1.在onCreate()方法执行完成之后,活动进入到Started状态。

2.活动进入Started状态之后,系统会调用onStart()方法。

3.这个方法的调用会使得活动进入用户可见状态,app会为活动进入前台以及用户交互做准备。

4.我们可以将维护UI界面的代码放入这个方法中。

5.也可以在这个方法中注册一个BroadcastReceiver来监听UI的改变。

3.onResume()

1️⃣Soon after onStart() finishes the system invokes onResume() callback method and the activity enter into resumed state.
2️⃣This is the state in which the app interacts with the user.
3️⃣The app stays in this state until something happens to take focus away from the app.Such as receiving a phone call, the user navigates to another activity, or the device screen's turning off.
4️⃣When an interruptive event occurs, the activity enters the Paused state,and the system invokes the onPause() callback.
5️⃣If the activity returns to the Resumed state from the Paused state, the system once again calls onResume() method.
6️⃣You can use this method to initialize components that you release during onPause() callback.

1.系统结束调用onStart()方法后,会调用onResume()方法,同时活动会进入到resumed状态。
2.这是app与用户交互的状态。
3.app会保持在这个状态直到焦点从app上移除。比如电话打入,用户操作进入另一个活动,或者设备屏幕关闭。
4.当一个中断事件发生,活动进入到Paused状态,然后系统会调用onPause()方法。
5.如果活动从Paused状态回到Resumed状态,系统会再次调用onResume()方法。
6.你可以在这个方法中初始化那些你在onPause()方法中释放掉的组件。

4.onPause()

1️⃣The system calls this method when the user leave your activity.
2️⃣Use the onPause() method to pause operations such animations and music playback that should not continue while the Activity is in the Paused state.
3️⃣You can use the onPause() method to release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.
4️⃣For example, if your application uses the Camera, the onPause() method is a good place to release it.

1.当用户离开活动时,系统会调用这个方法。
2.你应该使用onPause()方法来暂停那些不应该在Paused状态进行的操作,如动画和音乐播放。
3.在活动暂停时,你可以用onPause()方法来释放一些系统资源,如广播接收器(BroadcastReceivers),感应器(如GPS),或其他一些用户不需要,同时又消耗电量的资源。
4.举个例子,如果你的应用使用了Camera,onPause()方法就是一个用来释放它的好地方。

5.onStop()

1️⃣When your activity is no longer visible to the user, it has entered the Stopped state, and the system invokes the onStop() callback.
2️⃣The system may also call onStop() when the activity has finished running, and is about to be terminated.
3️⃣In the onStop() method, the app should release almost all resources that aren't needed while the user is not using it.
4️⃣For example, if you registered a BroadcastReceiver in onStart() to listen for changes that might affect your UI, you can unregister the broadcast receiver in onStop().

1.当你的活动不在对用户可见,它就会进入Stopped状态,同时系统就会调用onStop()回调。
2.系统也会在活动完成运行之后调用onStop(),同时活动也会终止。
3.在onStop()方法中,app会释放掉几乎所有不需要的资源。
4.举个例子,如果你在onStart()中注册了BroadcastReceiver来监听可能影响UI界面的改变,你可以在onStop()中注销掉这个广播接收器。

6.onDestroy()

1️⃣This method is Called before the activity is destroyed. This is the final call that the activity receives.
2️⃣The system either invokes this callback because the activity is finishing due to someone's calling finish(), or because the system is temporarily destroying the process containing the activity to save space.
3️⃣The onDestroy() callback releases all resources that have not yet been released by earlier callbacks such as onStop().

1.这个方法会在活动被销毁之前调用,它将是活动接收到的最后一个调用。
2.当活动结束,某个地方调用了finish()方法,或系统为了内存空间而临时结束掉活动,这个方法就会被调用。
3.这个方法会释放掉之前的方法(如onStop())没有释放的所有资源。

你可能感兴趣的:(Activity Lifecycle callbacks methods)