2017-07-31 63 views
0

我有一个可操作的启动画面活动。但是,如果用户点击返回按钮,并且在再次恢复应用程序后,我不想显示splashscreen。我怎样才能做到这一点 ?仅在新鲜开放时显示Splashscreen

SplashScreen.java

public class SplashScreen extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     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(); 

    } 
} 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.fardrop.radarso"> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher" 
     android:supportsRtl="true" 
     android:theme="@style/app_theme"> 

     <activity 
      android:name="com.fardrop.radarso.SplashScreen" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait" 
      android:theme="@android:style/Theme.Black.NoTitleBar" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

    <activity android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:launchMode="singleInstance" 
      android:alwaysRetainTaskState="true" 
      android:icon="@mipmap/ic_launcher" 
      android:screenOrientation="portrait" 
      android:theme="@style/AppTheme"> 
     </activity> 

    </application> 

</manifest> 
+2

共享PREF和检查是指https://stackoverflow.com/questions/20641580/show-splash-screen-one-time-only –

+1

店闪屏状态它在创建spalsh屏幕,如果用户已经访问它只是导航到主屏幕 –

回答

1

试试这个

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    setContentView(R.layout.activity_splash); 
    new Handler().postDelayed(new Runnable() {  
    @Override 
    public void run() { 
     startActivity(new Intent(SplashActivity.this, MainActivity.class)); 
     finish(); 
     // close this activity 

     /* or this 
     Intent intent = new Intent(LoginActivity.this, HomeScreenActivity.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(intent);*/ 

    } 
}, 3000);// time for spalsh screen 
+0

试过这两种方法,它仍然显示飞溅:/ – user3304007

1

低于SplashActivity代码只需使用;

new Handler().postDelayed(new Runnable() { 

        @Override 
        public void run() { 
         try { 
          Intent i = new Intent(SplashScreen.this, HomeScreenActivity.class); 
          startActivity(i); 
          finish(); 
         } 
         catch (final Exception e) 
         { 
          runOnUiThread(new Runnable(){ 
           @Override 
           public void run() { 
            Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show(); 
           } 
          }); 
         } 

        } 
       }, 2*1000); 
0

您在SharedPreferences中存储了您的可见标志,同时再次显示您从sharedPreferences获取值以显示SplashScreen。 您可以参考下面的回答进一步澄清 show splash screen one time only