Reputation: 359
I'm writing a small Android Game and I want to show a Splash screen before each Level and after a Game Over. Most tutorials I found only show Splash Screen implementations that run before the MainActivity is run.
In my case I would need to "run" the Splash Screen while the MainActivity is running. How can I achieve this?
I've already created a SplashActivity
class that extends AppCompatActivity according to a Tutorial, but I don't think it is right for my Problem.
public class SplashActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}, 5000);
}
}
The activity has the following xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.androidcodefinder.splashscreendemo.MainActivity"
android:background="@color/backgroundColor">
<ImageView
android:id="@+id/logo"
android:src="@mipmap/splash"
android:layout_width="120dp"
android:layout_height="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
In the MainActivity
class I start a GamePanel extends SurfaceView
that then starts a GameLoop.
@Override
public void run () {
Canvas canvas;
while (!this.exit) {
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas();
playAnimation(R.drawable.splash_gameover, R.anim.viewanimation);
synchronized (this.surfaceHolder) {
this.gamePanel.update();
this.gamePanel.render(canvas, lag);
}
} finally {
if (canvas != null) {
this.surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
Upvotes: 1
Views: 1604
Reputation: 2623
In my android game Anagram it is working fine, I used below code , U can check it out-
I created a xml like :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#fff"
tools:context=".SplashScreen">
<ImageView
android:id="@+id/imgLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:background="@drawable/splash" />
</LinearLayout>
Where @drawable/splash would be your desired background which U want to show
And below is SplashScreen code
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_TIME_OUT);
}
}
Upvotes: 0
Reputation: 7628
You can call
Intent i = new Intent(MainActivity.this, SplashActivity.class);
startActivity(i);
at anywhere you want in MainActivity
to bring up the SplashActivity
. However, you probably don't want the SplashActivity
to create another new MainActivtiy
. You can pass some message through Intent
to tell the SplashActivtiy
what to do.
In MainActivity
Intent i = new Intent(MainActivity.this, SplashActivity.class);
i.putExtra("START_NEW_MAIN",false)
startActivity(i);
In SplashActivity
public class SplashActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean startNewMain = getIntent().getBooleanExtra("START_NEW_MAIN",true)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
if(startNewMain)
{
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
}
finish();
}
}, 5000);
}
}
You can even completely remove the part that start MainActivity
in SplashActivity
if SplashActivity
is not your app's entry point. Simply:
In MainActivity
Intent i = new Intent(MainActivity.this, SplashActivity.class);
startActivity(i);
In SplashActivity
public class SplashActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, 5000);
}
}
Upvotes: 2