2014-09-04 74 views
0

我有2項活動。由於內容的原因,Android活動會產生黑屏

ActivityA從按鈕單擊開始activityB。

當按鈕被點擊時,我創建一個新的對話框,並顯示一個圖像作爲一種減少加載屏幕(不用進度條困擾)。

當activityB從ActivityA啓動對話框時被殺死,我立即在oncreate的頂部啓動一個新對話框。我爲活動B創造了很多東西。

創建一個adview,一些主視圖的文本並創建一個GLsurface渲染器。

問題是,當activityB的oncreate被調用時,我在兩個對話框之間出現了一個4秒的黑色屏幕。我想嘗試儘可能減少這種情況。

因此,無論如何都要讓活動B中的對話以自然的方式運行,而不是等待onCreate的其餘部分完成正在做的事情。將提供代碼,如果需要的話

更新。它是我的glsurface渲染器的創建,導致黑屏持續了很長時間。下面是活動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(); 
} 

}

+0

將代碼顯示在'onCrate'頂部的對話框。如需更多幫助,請發佈一些代碼。 – Gumbo 2014-09-04 09:00:57

回答

1

您可以使用AsyncTask這個。

只准備onCreate內的UI元素(獲取需要的視圖引用,顯示對話框),但將所有繁重的任務移動到AsyncTask,並從那裏更新UI(關閉對話框,填充視圖...)在onPostExecute一旦加載完成。如果需要,您甚至可以使用AsyncTask來顯示加載進度;)

+1

我同意,你應該總是使用AsyncTask長時間運行的任務,因爲它是爲了這樣的事情而做的。 – 2014-09-04 09:10:48

+0

@ 2Dee感謝您的回答,我用activityB代碼更新了我的問題,問題在於創建了GlsurfaceView,是否可以將其移至AsyncTask,並記住傳遞給渲染器的不同參數? – Rob85 2014-09-04 11:02:14

0

您可以使用

public static Dialog d; 
在活動A.

秀活動 舉動活動B 和aftar所有操作完成成功 您可以從活動B中消除該對話框。如

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

希望這會解決您的問題..

+0

感謝您的答案,但這似乎並沒有工作,對話似乎仍然開始新任務時被殺害,即使它是靜態的,我從來沒有解僱()它 – Rob85 2014-09-04 10:50:26

相關問題