为什么每次更改初始屏幕时我的应用程序都会从后台打开?
Why does my app keep opening from the background everytime a splash screen is changed?
我创建了一个包含 3 个启动画面的应用程序。它们出现并在 10 秒后消失,下一个取代它直到第三个结束然后主 activity 打开并且应用程序正常运行。
问题是,如果用户在这些启动画面中的任何一个期间将应用程序转到后台,10 秒后应用程序将自己返回到前台,即使用户正在使用另一个应用程序并显示下一个启动画面或主要 activity.
我查看了代码,似乎找不到任何可以解释这一点的内容。在 Android Studio 更新到 3.5 之前它工作正常,我不知道为什么会导致这个问题。
public class loadScreen extends AppCompatActivity {
private int SLEEP_TIMER = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_load_screen);
LogoLauncher logoLauncher = new LogoLauncher();
logoLauncher.start();
}
private class LogoLauncher extends Thread{
public void run(){
try{
sleep(3000 * SLEEP_TIMER);
}catch(InterruptedException e)
{
e.printStackTrace();
}
Intent intent = new Intent(loadScreen.this, createdby.class);
startActivity(intent);
loadScreen.this.finish();
}
}
@Override
public void onBackPressed() {
}
}
我希望如果在启动画面序列期间当用户 returns 将应用程序发送到后台时,它将从他们离开的地方恢复。
你的问题是,即使你的应用程序在后台,startActivity 也会被调用。这将在调用时打开您的应用程序。所以你需要在那个部分创建一些逻辑来检查是否允许调用 startActivity 方法。
编辑:
用于检查 activity 的挂起开始的代码。试试这个!
private static final String PENDING_LAUNCH_KEY = "PENDING_LAUNCH";
private boolean pendingLaunch;
private boolean activityPaused;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
pendingLaunch = savedInstanceState.getBoolean(PENDING_LAUNCH_KEY);
}
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_load_screen);
if (!pendingLaunch) {
LogoLauncher logoLauncher = new LogoLauncher();
logoLauncher.start();
}
}
@Override
protected void onResume() {
activityPaused = false;
if (pendingLaunch) {
pendingLaunch = false;
startAndFinish();
}
}
@Override
protected void onPause() {
activityPaused = true;
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
outState.putBoolean(PENDING_LAUNCH_KEY, pendingLaunch);
super.onSaveInstanceState(outState, outPersistentState);
}
private class LogoLauncher extends Thread{
public void run(){
try{
sleep(3000 * SLEEP_TIMER);
}catch(InterruptedException e)
{
e.printStackTrace();
}
if (activityPaused) pendingLaunch = true;
else startAndFinish();
}
}
private void startAndFinish() {
Intent intent = new Intent(loadScreen.this, createdby.class);
startActivity(intent);
finish();
}
the app will bring itself back to the front even if the user is using
another app
你的线程仍然运行即使你的应用程序进入后台!,
解决方案是,
您必须 terminate
onPause
方法中的那个线程
it will resume from when where they left it.
SplashActivity
public class SplashActivity extends AppCompatActivity {
private Handler handler = null;
private long SPLASH_TIMEOUT = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
handler = new Handler();
}
private Runnable splashRunnable = new Runnable() {
@Override
public void run() {
Intent mySuperIntent = new Intent(SplashActivity.this, SplashActivity1.class);
startActivity(mySuperIntent);
finish();
}
};
@Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(splashRunnable);
}
@Override
protected void onResume() {
super.onResume();
handler.postDelayed(splashRunnable, SPLASH_TIMEOUT);
}
}
SplashActivity1 只有 SplashActivity 的相同代码 intent
会改变。
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SplashActivity1 extends AppCompatActivity {
private Handler handler = null;
private long SPLASH_TIMEOUT = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash1);
handler = new Handler();
}
private Runnable splashRunnable = new Runnable() {
@Override
public void run() {
Intent mySuperIntent = new Intent(SplashActivity1.this,
MainActivity.class);
startActivity(mySuperIntent);
finish();
}
};
@Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(splashRunnable);
}
@Override
protected void onResume() {
super.onResume();
handler.postDelayed(splashRunnable, SPLASH_TIMEOUT);
}
}
我创建了一个包含 3 个启动画面的应用程序。它们出现并在 10 秒后消失,下一个取代它直到第三个结束然后主 activity 打开并且应用程序正常运行。
问题是,如果用户在这些启动画面中的任何一个期间将应用程序转到后台,10 秒后应用程序将自己返回到前台,即使用户正在使用另一个应用程序并显示下一个启动画面或主要 activity.
我查看了代码,似乎找不到任何可以解释这一点的内容。在 Android Studio 更新到 3.5 之前它工作正常,我不知道为什么会导致这个问题。
public class loadScreen extends AppCompatActivity {
private int SLEEP_TIMER = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_load_screen);
LogoLauncher logoLauncher = new LogoLauncher();
logoLauncher.start();
}
private class LogoLauncher extends Thread{
public void run(){
try{
sleep(3000 * SLEEP_TIMER);
}catch(InterruptedException e)
{
e.printStackTrace();
}
Intent intent = new Intent(loadScreen.this, createdby.class);
startActivity(intent);
loadScreen.this.finish();
}
}
@Override
public void onBackPressed() {
}
}
我希望如果在启动画面序列期间当用户 returns 将应用程序发送到后台时,它将从他们离开的地方恢复。
你的问题是,即使你的应用程序在后台,startActivity 也会被调用。这将在调用时打开您的应用程序。所以你需要在那个部分创建一些逻辑来检查是否允许调用 startActivity 方法。
编辑: 用于检查 activity 的挂起开始的代码。试试这个!
private static final String PENDING_LAUNCH_KEY = "PENDING_LAUNCH";
private boolean pendingLaunch;
private boolean activityPaused;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
pendingLaunch = savedInstanceState.getBoolean(PENDING_LAUNCH_KEY);
}
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_load_screen);
if (!pendingLaunch) {
LogoLauncher logoLauncher = new LogoLauncher();
logoLauncher.start();
}
}
@Override
protected void onResume() {
activityPaused = false;
if (pendingLaunch) {
pendingLaunch = false;
startAndFinish();
}
}
@Override
protected void onPause() {
activityPaused = true;
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
outState.putBoolean(PENDING_LAUNCH_KEY, pendingLaunch);
super.onSaveInstanceState(outState, outPersistentState);
}
private class LogoLauncher extends Thread{
public void run(){
try{
sleep(3000 * SLEEP_TIMER);
}catch(InterruptedException e)
{
e.printStackTrace();
}
if (activityPaused) pendingLaunch = true;
else startAndFinish();
}
}
private void startAndFinish() {
Intent intent = new Intent(loadScreen.this, createdby.class);
startActivity(intent);
finish();
}
the app will bring itself back to the front even if the user is using another app
你的线程仍然运行即使你的应用程序进入后台!,
解决方案是,
您必须 terminate
onPause
方法中的那个线程
it will resume from when where they left it.
SplashActivity
public class SplashActivity extends AppCompatActivity {
private Handler handler = null;
private long SPLASH_TIMEOUT = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
handler = new Handler();
}
private Runnable splashRunnable = new Runnable() {
@Override
public void run() {
Intent mySuperIntent = new Intent(SplashActivity.this, SplashActivity1.class);
startActivity(mySuperIntent);
finish();
}
};
@Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(splashRunnable);
}
@Override
protected void onResume() {
super.onResume();
handler.postDelayed(splashRunnable, SPLASH_TIMEOUT);
}
}
SplashActivity1 只有 SplashActivity 的相同代码 intent
会改变。
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SplashActivity1 extends AppCompatActivity {
private Handler handler = null;
private long SPLASH_TIMEOUT = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash1);
handler = new Handler();
}
private Runnable splashRunnable = new Runnable() {
@Override
public void run() {
Intent mySuperIntent = new Intent(SplashActivity1.this,
MainActivity.class);
startActivity(mySuperIntent);
finish();
}
};
@Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(splashRunnable);
}
@Override
protected void onResume() {
super.onResume();
handler.postDelayed(splashRunnable, SPLASH_TIMEOUT);
}
}