英文:
Android: Black screen flashes when activity resumes
问题
我遇到了一个关于我创建的TabbedActivity
的问题。
我注意到,如果我将我的应用程序放在后台,然后再将其切换到前台,布局会在重新出现之前短暂变黑。看起来好像活动正在被重新创建,但我已经确认在应用程序恢复时并没有调用onCreate()
。
我在活动的onResume()
和onStart()
方法中设置了断点,以查看黑屏发生的时间点。当应用程序回到前台时,首先调用onStart()
,然后是onResume()
,然后屏幕在短暂的黑屏后闪烁变亮。之后,布局重新出现。如图所示:
我想指出,除了调用它们的超类之外,此活动的onCreate()
和onResume()
方法都是空的。在上面的GIF中显示的片段甚至没有覆盖onResume()
或onCreate()
方法。此外,在我应用程序中的其他非选项卡活动中,不会出现此行为 - 它只会发生在TabbedActivity
中。
如果需要更多信息,请告诉我,我将乐意提供。我只是不太确定问题可能出在哪里。
编辑:
我已经意识到,当我注释掉另一个片段的onResume()
方法的内容时,这种情况不再发生。以下是该方法的内容:
@Override
public void onResume() {
super.onResume();
System.out.println("RESUME");
if (mShowCamera != null)
{
mShowCamera.camera = null;
}
mCamera = Camera.open();
mShowCamera = new ShowCamera(getContext(), mCamera);
mMainCamLayout = (FrameLayout)getView().findViewById(R.id.camLayout);
mMainCamLayout.addView(mShowCamera);
Camera.Parameters cameraParameters = mCamera.getParameters();
cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
if (cameraParameters.getSupportedFocusModes() != null &&
cameraParameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
{
mCamera.setParameters(cameraParameters);
}
}
英文:
I am having an issue with a TabbedActivity
I have created.
I notice if I put my application in the background and then bring it back to the foreground, the screen goes black for a moment before the layout reappears. It looks as if the activity is being recreated, but I have confirmed that onCreate()
is NOT being called when the app is resumed.
I set breakpoints in the onResume()
and onStart()
methods in the activity to see when the black screen occurs. When the app comes back to the foreground, onStart()
is called, then onResume()
, and THEN the screen flashes black for a split second. After this, the layout reappears. This is shown here:
I would like to note that this activity's onCreate()
and onResume()
methods are empty besides the calls to their superclasses. The fragment that is shown in the above gif doesn't even override the onResume()
or onCreate()
methods. Also, in other activities in my app that are NOT tabbed activities, this behavior does not occur - it only occurs in the TabbedActivity
.
Please let me know if more information is needed and I will gladly provide it. I'm just not exactly sure where this problem could possibly be occurring.
EDIT:
I've realized when I comment out the contents of ANOTHER FRAGMENT'S onResume()
method, this no longer occurs. Here is what that method looks like:
@Override
public void onResume() {
super.onResume();
System.out.println("RESUME");
if (mShowCamera != null)
{
mShowCamera.camera = null;
}
mCamera = Camera.open();
mShowCamera = new ShowCamera(getContext(), mCamera);
mMainCamLayout = (FrameLayout)getView().findViewById(R.id.camLayout);
mMainCamLayout.addView(mShowCamera);
Camera.Parameters cameraParameters = mCamera.getParameters();
cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
if (cameraParameters.getSupportedFocusModes() != null &&
cameraParameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
{
mCamera.setParameters(cameraParameters);
}
}
答案1
得分: 0
这个问题已经解决。问题是当我的应用程序重新获得焦点时,有大量的工作正在进行。这导致加载该活动所需的时间增加。
这个活动附带了一些片段。导致这个问题的一个因素是尝试缓存太多的片段,像这样:
ViewPager mViewPager = findViewById(R.id.view_pager);
mViewPager.setOffscreenPageLimit(4);
减少屏幕外页面的数量使得活动的初始加载明显更快。
如果您的活动加载时间很长,请尝试减少它在其OnCreate和OnResume方法中需要做的工作。
英文:
This has been resolved. The issue was that there was a lot of work being done when my application came back into focus. This caused the time required to load the activity to increase.
This activity has a few fragments attached to it. One contributing factor to this problem was trying to cache too many fragments like so:
ViewPager mViewPager = findViewById(R.id.view_pager);
mViewPager.setOffscreenPageLimit(4);
Minimizing the number of off-screen pages made the initial loading of my activity noticeably faster.
If your activity is taking a long time to load, try reducing the amount of work it has to do in its OnCreate and OnResume methods.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论