为了账号安全,请及时绑定邮箱和手机立即绑定

为什么每次更改启动屏幕时我的应用程序都会从​​后台打开?

为什么每次更改启动屏幕时我的应用程序都会从​​后台打开?

白衣非少年 2023-06-21 13:30:46
我创建了一个包含 3 个启动画面的应用程序。它们在 10 秒后出现并消失,下一个取代它,直到第三个结束,然后主活动打开,应用程序正常运行。问题是,如果用户在任何这些启动屏幕期间将应用程序发送到后台,则 10 秒后,即使用户正在使用另一个应用程序,应用程序也会回到前台并显示下一个启动屏幕或主要活动。我查看了代码,似乎找不到任何可以解释这一点的内容。在 Android Studio 更新到 3.5 之前它工作正常,我不知道为什么会导致这个问题。public class  loadScreen extends AppCompatActivity {private int SLEEP_TIMER = 3;@Overrideprotected 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();    }}@Overridepublic void onBackPressed() {}}我希望如果应用程序在启动画面序列期间在用户返回时被发送到后台,它将从他们离开的地方恢复。
查看完整描述

2 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

您的问题是即使您的应用程序在后台,也会调用 startActivity。这将在调用时打开您的应用程序。因此,您需要在该部分中创建一些逻辑来检查是否允许调用 startActivity 方法。


编辑:用于检查活动的待启动的代码。试试这个!


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();

}


查看完整回答
反对 回复 2023-06-21
?
慕虎7371278

TA贡献1802条经验 获得超4个赞

即使用户正在使用另一个应用程序,该应用程序也会将自己带回前台

即使您的应用程序进入后台,您的线程仍在运行!解决方案是,您必须在方法中使用terminate该线程onPause

它将从他们离开的地方恢复。

飞溅活动

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);


}

}


查看完整回答
反对 回复 2023-06-21
  • 2 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信