2017-01-17 5 views
0

나는 내 응용 프로그램에 시작 화면을 추가하고 내 코드는 다음과 같습니다초기 화면 활동

내 매니페스트
public class SplashActivity extends AppCompatActivity { 

    public static final int DELAY_MILLIS = 2000;//for testing i use 5 seconds 
    private Handler handler = null; 

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

     handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       Intent intent = new Intent(SplashActivity.this, CurrencyExchangeActivity.class); 
       startActivity(intent); 
       finish(); 
      } 
     }, DELAY_MILLIS); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     handler.removeCallbacksAndMessages(null); 
    } 
} 

: 나는 경우

:

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

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

    <application 
     android:name=".application.CurrencyApp" 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

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

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

     <activity android:name=".screens.main.CurrencyExchangeActivity"> 
     </activity> 
    </application> 
</manifest> 

그래서 내가 다음 문제에 직면 스플래시 화면을로드 할 때 홈 버튼으로 내 앱을 숨긴 다음 앱을 다시 열면 스플래시 화면 액티비티가 CurrencyExchangeActivity으로 전화하지 않습니다. 메소드 onCreate()이 한 번만 호출 되었기 때문에 나타납니다. 그러나 앱을 다시 열면 스플래시 화면이 다시 표시되기 때문에 onResume()에 넣을 수 없습니다. 하지만 Google의 앱 (지도, 시트 등 ...)과 같이 CurrencyActivity을 표시하고 싶습니다.

+0

도 –

답변

0

당신이 할 수있는 당신이 당신의 Activitythemebackground으로 스플래쉬 화면의 이미지를 설정하는 간단하기 때문이다. 창 수준의 배경이 네비게이션이나 무엇이든 관계없이 나타납니다.

앱스 라이프에서 스플래시 화면을 한 번만 표시하려면 not the right way에서 시작해야하며 이미지를 레이아웃의 일부로 사용하고 setContentView을 사용하여 해당 레이아웃을 부 풀리면됩니다. 이제 다시 앱에 올 때 이 splash activity이되기 전에 CurrencyExchangeActivity으로 전화를 걸면 검은 색 창 배경이 나타나고 바로 CurrencyExchangeActivity으로 표시됩니다.

이 말이 맞는 것인지 알려주세요. 필요한 경우 더 자세히 설명 할 수 있습니다.

+0

Google도 올바른 방식이 아닌 것으로 생각하십니까? – Vdovin

+0

@Vdovin 당신이 말하는 Google 앱은 무엇입니까? –

+0

예, Google지도와 같은 것, Google 시트 – Vdovin

0

이 방법을 시도해보십시오. 나는 당신의 코드를 테스트하지 않았다. 스플래시를 사용하여 내 영향력은 아래와 같이 될 것입니다 :

public class SplashActivity extends Activity { 
private static final String TAG = SplashActivity.class.getSimpleName(); 
Handler handler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     loadNext(); 
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_splash); 


    new Thread() { 
     @Override 
     public void run() { 
      try { 
       sleep(1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      handler.sendEmptyMessage(1); 
     } 
    }.start(); 
} 


protected void loadNext() { 
    Intent intent = new Intent(this, NextActivity.class); 
    startActivity(intent); 
    finish(); 
} 
} 
+0

를 매니페스트 파일을 추가합니다 대답 주셔서 감사합니다,하지만 그것은 나를 위해 작동하지 않습니다. 5 초 후 CurrencyActivity가 자동으로 열립니다. – Vdovin

+0

"스플래시 화면 Activity not Call CurrencyExchangeActivity"currencyExchangeActivity에 대한 탐색에 문제가 있다고 생각했습니다. –

+0

아니요, onCreate 메소드가 onStop의 핸들러에서 콜백을 제거하므로 Currency Exchange Activity를 호출하지 않습니다. onCreate를 호출하지는 않지만 onResume에 핸들러를 두지 않은 이유를 썼습니다. – Vdovin

0

당신은 어쩌면 당신은 다음과 같은 시도 할 수는

+0

레이어를 통해 처리합니다. https://www.bignerdranch.com/blog/splash-screens-the-right-way/ – Vdovin

0

을 작동이 추가

setContentView(R.layout.splash);

을 추가 잊은 :

@Override 
protected void onResume() { 
    super.onResume(); 
    Intent intent = new Intent(this, CurrencyExchangeActivity.class); 
    startActivity(intent); 
    finish(); 
} 

이후 앱을 다시 열 때 지연을 건너 뜁니다. 홈 버튼을 누르십시오.

+0

홈 버튼으로 앱을 숨긴 다음 다시 엽니 다. 다시 스플래시 화면을 볼 수 있지만 CurrencyActivity를보고 싶습니다 – Vdovin

0

일반적으로 postDelayed Runnable을 사용합니다.

import android.content.Intent; 
import android.os.Bundle; 
import android.os.Handler; 
import android.support.v7.app.AppCompatActivity; 

public class MainActivity extends AppCompatActivity { 

    private Handler handler = new Handler(); 

    private Runnable runnable = new Runnable() { 
     @Override 
     public void run() { 
      Intent intent = new Intent(this, NextActivity.class); 
      startActivity(intent); 
      finish(); 
     } 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     // 5 sec 
     handler.postDelayed(runnable, 5000); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     // 5 sec 
     handler.removeCallbacks(runnable); 
    } 
} 
+0

스플래시 화면을로드 할 때 앱을 숨긴 다음 다시 열면 스플래시 화면이 다시 나타납니다. 그래서 그것은 나를 위해 작동하지 않습니다 – Vdovin

+0

당신은 onResume()에서 실행 가능한 postdelay 경우이 문제가되지 않았습니다. 코드의 버그는 runnable onCreate()를 실행하는 것입니다. – mcatta

0

CurrencyActivity에 스플래시 화면을 유지하는 것이 좋습니다. 다음은 의사 코드입니다.

코드에 적절한 설명을 추가했습니다. 확인해주십시오.

public class CurrencyActivity extends AppCompatActivity { 

    public static final int DELAY_MILLIS = 2000; //for testing i use 5 seconds 
    private Handler handler = null; 
    private boolean showSplash = true; // True by default 
    private ImageView splashImage; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.currency_activity); 

     // Get a image overlaying the other views in your currency layout 
     splashImage = (ImageView) findViewById(R.id.splash); 

     if(showSplash) showSplashScreen(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     handler.removeCallbacksAndMessages(null); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     showSplash = false; // Set the variable to false when you take this in background 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     // Check if the variable is false and then set the visibility to GONE 
     if(!showSplash) splashImage.setVisibility(View.GONE); 
    } 

    private void showSplashScreen() { 
     handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       splashImage.setVisibility(View.GONE); 
       showSplash = false; 
      } 
     }, DELAY_MILLIS); 
    } 
} 
0

manifest--

<?xml version="1.0" encoding="utf-8"?> 

를 들어이해서 돌출

SplashScreenActivity-

public class SplashActivity extends AppCompatActivity { 

public static final int DELAY_MILLIS = 2000;//for testing i use 5 seconds 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_splash); 

    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      Intent intent = new Intent(SplashActivity.this, CurrencyExchangeActivity.class); 
      startActivity(intent); 
      finish(); 
     } 
    }, DELAY_MILLIS); 
} 

}

시도

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".SplashActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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




</application> 

난 당신이 홈 화면 버튼을 누를 때 시작 화면은 다음이 일하는 것이 다시 표시되는 문제가 있다면이 코드를 테스트....