Splash Animation在Android中只显示一次



每次我使用我的应用程序时,闪屏应该工作,但是一旦我从任务管理器中清除应用程序并重新启动它。

动画没有出现,它直接进入登录屏幕,没有动画发生。

package com.example.promilek;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
@SuppressLint("CustomSplashScreen")
public class SplashScreen extends AppCompatActivity {
private static final int SPLASH_SCREEN = 2000;
//Variables
Animation topAnim;
ImageView ImageViewButla;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow() .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash_screen);
topAnim= AnimationUtils.loadAnimation(this, R.anim.top_animation);
ImageViewButla = findViewById(R.id.imageViewButla);
ImageViewButla.setAnimation(topAnim);
new Handler() .postDelayed(() -> {
Intent intent = new Intent(SplashScreen.this, Login.class);
startActivity(intent);

finish();
},SPLASH_SCREEN);
}
}

在我的解决方案中,我将使用Thread类来完成此操作。至少这会给你在活动生命周期方法中更多的控制。

我还包含了一个SessionManager来帮助你,如果你现在或以后需要它。

带有动画的Activity

@SuppressLint("CustomSplashScreen")
public class SplashScreen extends AppCompatActivity {
private static final int SPLASH_SCREEN = 2000;

//Variables
Animation topAnim;
ImageView ImageViewButla;
private Thread counterThread;
Context mContext;
private SessionManager sessionManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow() .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash_screen);

mContext = getApplicationContext(); // Get application context

topAnim = AnimationUtils.loadAnimation(this, R.anim.top_animation);
ImageViewButla = findViewById(R.id.imageViewButla);
ImageViewButla.setAnimation(topAnim);
sessionManager = new SessionManager(mContext); // Initialize Session manager object

// Counter thread
counterThread = new Thread() {
@Override
public void run() {
try {
int wait = 0; // Thread wait time
// Loop
while (wait < SPLASH_SCREEN) {
sleep(100); // Sleep
wait += 100;
}
Intent intent;
if (sessionManager.isSignedIn()) {
// Launch MainActivity
intent = new Intent(SplashActivity.this, MainActivity.class);
} else {
// Launch Signin or SignUp activity
intent = new Intent(SplashActivity.this,
SignInSignUpActivity.class);
}
startActivity(intent); // Start activity
finish(); // Exit Activity
} catch (Exception ignored) {
} finally {
finish(); // Exit Activity
}
}
};
}
}

@Override
public void onBackPressed() {
super.onBackPressed(); // Exit
counterThread.start(); // Start thread
}
@Override
public void onResume() {
super.onResume();
counterThread.start(); // Start thread
}
@Override
protected void onStop() {
super.onStop();
counterThread.interrupt(); // Interrupt thread on activity exit
}
@Override
protected void onDestroy() {
super.onDestroy();
counterThread.interrupt(); // Interrupt thread on activity exit
}

SessionManager类下面是会话管理器 的代码
public class SessionManager {
private static final String KEY_IS_SIGNED_IN = "isSignedIn";
private static String PREFERENCE_NAME; // Shared Preference File Name
// Shared Preference
private final SharedPreferences sharedPreferences;
private final SharedPreferences.Editor editor;
// Shared Preference mode
int PRIVATE_MODE = 0;
@SuppressLint("CommitPrefEdits")
public SessionManager(Context context) {
this.sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, PRIVATE_MODE);
this.editor = sharedPreferences.edit();
PREFERENCE_NAME = context.getResources().getString(R.string.app_name) +
"_SessionManagerPreference";
}
public boolean isSignedIn() {
return sharedPreferences.getBoolean(KEY_IS_SIGNED_IN, false);
}
public void setSignedIn(boolean isLoggedIn) {
editor.putBoolean(KEY_IS_SIGNED_IN, isLoggedIn);
editor.commit(); // Commit Changes
}
}

最新更新