2017-08-02 11 views
1

私はアプリの新スタートでのみ表示されているスプラッシュ画面があります。ユーザーがボタンを押してアプリを再起動すると、スプラッシュは表示されません。スプラッシュが表示されない場合、アプリを開くときには1〜2秒の黒い画面が表示されます。ここに私のsplashactivity Javaファイルです。アプリを開くときに黒い画面を削除する方法

public class SplashScreen extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
if(!prefs.getBoolean("first_time", false)) // if first time, show splash 
{ 

    SharedPreferences.Editor editor = prefs.edit(); 
    editor.putBoolean("first_time", true); 
    editor.commit(); 


    setContentView(R.layout.activity_splash); 


    Thread t = new Thread() { 
     public void run() { 
      try { 
       int time = 0; 
       while (time < 4000) { 
        sleep(100); 
        time += 100; 
       } 
      } 
      catch (InterruptedException e) { 
       // do nothing 
      } 
      finally { 

       Intent i = new Intent(SplashScreen.this, MainActivity.class); 
       startActivity(i); 
       finish(); 
      } 
     } 
    }; 
    t.start(); 
} 
else // if not first time, dont show splash 
{ 
    setContentView(R.layout.activity_splash); 
    Intent i = new Intent(SplashScreen.this, MainActivity.class); 
    startActivity(i); 
    finish(); 
} 

どうすればこの問題を解決できますか?

+0

この試す:あなたのランチャーの活動では

android:theme="@style/AppTheme.splashScreenLauncher"> 

:あなたのランチャーアクティビティを設定マニフェストに

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque"> <!-- The background color, preferably the same as your normal theme --> <item android:drawable="@android:color/white"/> <item> <bitmap android:src="@drawable/SPLASHIMAGE" android:gravity="center"/> </item> </layer-list> 

、 'アンドロイド:テーマ=" @アンドロイド:あなたの 'manifest.xml'の' style/Theme.Translucent''と同じ質問 –

+0

同じ質問をお試しくださいhttps://stackoverflow.com/questions/8817568/how-to-avoid-black-screen-in-android-while-my-app -is-loading –

答えて

1

あなたがチェックしているので、それはスプラッシュ画面を表示する前にアプリを開いて、「初めて」の場合:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
if(!prefs.getBoolean("first_time", false)) { 
    // Show splash 
} else { 
    // Don't show splash 
} 

をこれは、最初の場所にスプラッシュ画面を作成するのではなく、right wayです。ユーザーは、タイマーを作成してスプラッシュ画面を待つべきではありません。 Insteadの場合は、アプリを読み込むまでスプラッシュ画面を表示する必要があります。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- A solid background fill --> 
    <item 
     android:drawable="@color/gray"/> 
    <item> 

    <!-- A centered logo --> 
    <bitmap 
     android:gravity="center" 
     android:src="@mipmap/ic_launcher"/> 
    </item> 

</layer-list> 

次に、あなたがあなたで使用するテーマのbackgroundとして、この層-リストを使用:このあなたのスプラッシュ活動の背景として使用するために、単純なレイヤーリストの描画可能を作成する必要がありますを行うには

スプラッシュアクティビティ:

<activity android:name=".SplashScreen" 
    android:theme="@style/SplashTheme"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 

     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

<resources> 

    <!-- Base application theme --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
    </style> 

    <!-- Splash Screen theme --> 
    <style name="SplashTheme"> 
     <item name="android:background">@drawable/background_splash</item> 
     <item name="android:windowAnimationStyle">@null</item> 
    </style> 

</resources> 

はマニフェストでスプラッシュアクティビティにそのテーマを適用します。

public class SplashScreen extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Intent intent = new Intent(this, MainActivity.class); 
     startActivity(intent); 
     finish(); 
    } 
} 

これはに不必要なロード時間が追加されますよう、あなたのスプラッシュ活動ではありませんコールsetContentView()を行うていることを確認してください:

はその後、最終的に、あなたはスプラッシュ活動にしなければならないすべては、あなたのMainActivityを開始していますあなたのスプラッシュスクリーン。

+0

より良いアプローチ、ユーザーがアプリを開くたびにスプラッシュが表示されますか?私のアプリは1秒間にロードされ、これはスプラッシュが1秒間しか表示されません、それは不要ですか?このテクニックを使用してスプラッシュミニマムを3秒間表示して初めて新しく始動した場合はどうですか? – user198989

+0

@ user198989これはリンク先の[記事](https://www.bignerdranch.com/blog/splash-screens-the-right-way/)に記載されているように、ベストプラクティスではないためです。なぜユーザーを待たせるのでしょうか?モバイルアプリの目的は高速ではありませんか? – Bryan

+0

@ user19889はい、この方法では、アプリが開かれるたびにスプラッシュ画面が表示されます(ただし、中断後は前面に表示されません)。 – Bryan

1

アプリのスタイルでこれを追加します。

<item name="android:windowDisablePreview">true</item> 
1
/Just use the below code Snipeet/ 

Add following code in Style.xml file 

<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowActionBar">false</item> 
    <item name="android:windowFullscreen">true</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowIsTranslucent">true</item> 

</style> 

And then use this style in AndroidManifest inside application Tag 
Like this: 
android:theme="@style/Theme.Transparent" 
1

アプリケーションのメインテーマカラーとしてスプラッシュスクリーンの背景色を指定する必要があります。自分の価値観-V21に値/ style.xml `

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
<style name="SplashScreenTheme" parent="AppBaseTheme"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
    <item name="android:windowBackground">@color/white</item> 
    <item name="colorPrimaryDark">@color/white</item> 
</style> 

セットスタイルで

<activity 
     android:name=".SplashScreen" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:theme="@style/SplashScreenTheme"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

セットスタイル/ style.xml

<style name="SplashScreenTheme" parent="AppBaseTheme"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
    <item name="android:windowBackground">@color/white</item> 
    <item name="android:colorPrimaryDark">@color/white</item> 
</style> 
1

追加私はあなたにcold startをお勧めします@Bryan 。たとえば、私はあなたのStyle.xmlではこの

のようでした:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 

<style name="AppTheme.splashScreenLauncher"> 
    <item name="android:windowBackground">@drawable/splash_screen</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 

</style> 

splash_screen。XML:

setTheme(R.style.AppTheme); // IMPORTANT BEFORE super.onCreate 
super.onCreate(savedInstanceState); 
関連する問題