简体   繁体   English

如何在自定义视图中管理活动生命周期?

[英]How to manage activity lifecycle in custom view?

Recently I was making a custom view in Android. 最近,我在Android中进行自定义视图。

Like this: 像这样:

the capture of the custom view before rotating 旋转之前捕获自定义视图

but when I rotate the phone screen, the app execute onPause onStop onDestroy then execute onCreate onStart onResume onMeasure onDraw , so it invalidate the view from the 0 of x coordinate. 但是当我旋转手机屏幕时,该应用程序执行onPause onStop onDestroy然后执行onCreate onStart onResume onMeasure onDraw ,因此它使x坐标的0值无效。

Like this: 像这样:

the capture of the custom view after rotating 旋转后捕获自定义视图

I want to store the value of scroll X to SharePreferences , but I really don't know how to manage the Activity Lifecycle in the custom view. 我想将滚动X的值存储到SharePreferences ,但是我真的不知道如何在自定义视图中管理活动生命周期。

Adding to swapnil's answer 添加到swapnil的答案

sharedPrefernece or Bundle really does not matter. sharedPrefernece或Bundle确实无关紧要。 If you want to persist state even after app close use sharedPref otherwise bundle. 如果您想在应用程序关闭后仍保持状态,请使用sharedPref否则进行捆绑。

You probably need to use horizontal scrollView as your custom view might not fit in portrait mode . 您可能需要使用horizontal scrollView因为自定义视图可能不适合portrait mode For enhanced user experience if you think portrait does not work well as user needs to scroll back and forth a lot you can also restrict orientation to landscape . 为了增强用户体验,如果您认为纵向效果不佳,因为用户需要来回滚动很多,则还可以将方向限制为landscape In your manifest file add this attribute to activity node. manifest文件中,将此属性添加到活动节点。

android:screenOrientation="landscape"

instead of SharedPreferences you can use function onSaveInstanceState() and onRestoreInstanceState() 可以使用onSaveInstanceState()onRestoreInstanceState()函数代替SharedPreferences

onSavedInstanceState : onSavedInstanceState:

Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both).

This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle).

Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.

The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself.

If called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().

onRestoreInstanceState : onRestoreInstanceState:

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle).

You can find more examples at 您可以在以下位置找到更多示例

Recreating Activity 重新创建活动

How to use 如何使用

Sample Example 样例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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