简体   繁体   English

Android活动由于内容而产生黑屏

[英]Android activity produces black screen because of the content

I have 2 activities. 我有2个活动。

ActivityA starts activityB from a button click. ActivityA通过单击按钮来启动activityB。

When the button is clicked i create a new dialog and display an image as a kind of cut down loading screen (not bothered with progress bars). 单击该按钮后,我将创建一个新对话框,并将图像显示为一种缩减的加载屏幕(不受进度条的干扰)。

when activityB starts the dialog from ActivityA is killed and i immediately start a new one at the top of oncreate. 当activityB启动时,来自ActivityA的对话框被杀死,我立即在oncreate的顶部启动一个新对话框。 My oncreate for activityB does quite a few things. 我对activityB的oncreate做了很多事情。

Creates an adview, some text for the main view and creates a GLsurface renderer. 创建一个adview,为主视图添加一些文本,并创建GLsurface渲染器。

The problem is that when activityB's oncreate is called i get a 4 second black screen between my two dialogs. 问题是,当调用activityB的oncreate时,我在两个对话框之间出现了4秒的黑屏。 I would like to try and reduce this as much as i can. 我想尽量减少这种情况。

So is there anyway to have the dialog in activity B run immdeiately rather than waiting for the rest of the onCreate to finish doing what it is doing. 因此,是否有必要使活动B中的对话框立即运行,而不是等待onCreate的其余部分完成其工作。 Will provide code if need be 如果需要将提供代码

UPDATE. UPDATE。 it is the creation of my glsurface renderer that is causing the black screen for so long. 正是我的glsurface渲染器的创建导致了黑屏这么长时间。 Here is the Activity B code 这是活动B代码

public class ActivityB extends BaseGameActivity
{
/** Hold a reference to our GLSurfaceView */
private MyGLSurfaceView mGLSurfaceView;
public GamePlay GamePlay;
public GoogleApiClient mGoogleApiClient = null;
private AdView adView;
private static final String AD_UNIT_ID = "******************************";
private Button RotateButton;

@Override
public void onCreate(Bundle savedInstanceState)
{       
    super.onCreate(savedInstanceState);
    // load screen dialog
     Dialog loader_dialog = new Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        loader_dialog.setContentView(R.layout.loading_screen);
        loader_dialog.show();
    // Create an ad.
    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    RelativeLayout layout = new RelativeLayout(this);
    layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,    LayoutParams.MATCH_PARENT));

    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice("*****************************").build();
    // Start loading the ad in the background.
    adView.loadAd(adRequest);

    final TextView Score = new TextView(this);
    Score.setText(" 0");
    RelativeLayout.LayoutParams scoreParams = new 
    RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    Score.setLayoutParams(scoreParams);

    Typeface tf = Typeface.createFromAsset(getAssets(),"Fonts/test.ttf");
    Score.setTextSize(getResources().getDimension(R.dimen.textsize));
    Score.setTypeface(tf);
    Score.setTextColor(Color.parseColor("#FDAA03"));

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
    View userInterface = inflater.inflate(R.layout.user_interface, null);

    GameButton = (Button) userInterface.findViewById(R.id.gamebutton);

    GameButton.setOnTouchListener(new OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            //irrelivant game stuff

        }
    });

    // Check if the system supports OpenGL ES 2.0.
    final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
    final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;

    if (supportsEs2)
    {

        // Request an OpenGL ES 2.0 compatible context.
        mGLSurfaceView = new MyGLSurfaceView(this); new GLSurfaceView(this);
        mGLSurfaceView.setEGLContextClientVersion(2);           
        final DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);          
        // Set the renderer to our demo renderer, defined below.
        mGoogleApiClient = getApiClient();

        RelativeLayout.LayoutParams adParams = 
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        layout.addView(mGLSurfaceView);
        layout.addView(userInterface);
        layout.addView(Score);
        layout.addView(adView, adParams);

        mGLSurfaceView.setRenderer(new MyRenderer(this, Score,  mGoogleApiClient),displayMetrics);


        //Set main renderer             
        setContentView(layout); 


    }
    else
    {
        // This is where you could create an OpenGL ES 1.x compatible
        // renderer if you wanted to support both ES 1 and ES 2.
        return;
    }   

}   


@Override
protected void onResume() 
{
    // The activity must call the GL surface view's onResume() on activity onResume().
    super.onResume();
    mGLSurfaceView.onResume();
}

@Override
protected void onPause()
{
    // The activity must call the GL surface view's onPause() on activity onPause().
    super.onPause();
    mGLSurfaceView.onPause();
}

} }

You could use an AsyncTask for this. 您可以为此使用AsyncTask

Only prepare the UI elements inside onCreate (get references to views you need, show dialog) but move all the heavy lifting tasks to an AsyncTask , and update the UI (dismiss dialog, populate views, ...) from there in onPostExecute once the loading is done. 仅在onCreate内准备UI元素(获取所需视图的引用,显示对话框),然后将所有繁重的任务移至AsyncTask ,然后在onPostExecute从那里更新UI(关闭对话框,填充视图,...)。加载完成。 You could even use the AsyncTask to show loading progress if needed ;) 如果需要,您甚至可以使用AsyncTask来显示加载进度;)

You can use a 您可以使用

public static Dialog d;

in Activity A. show in activity a move to activity B and aftar all the operation done successfull you can dismiss that dialog from activity B. like 在活动A中。在活动中显示移至活动B,然后成功完成所有操作,您可以从活动B中取消该对话框。

if(A.d.isshowing()){
  A.d.dismiss();
}

Hope this will solve your problem.. 希望这能解决您的问题。

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

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