闪屏问题

发布于 2024-11-05 13:04:22 字数 754 浏览 2 评论 0原文

我正在使用以下代码创建启动屏幕,当我按后退键时,应用程序将移动到主屏幕,并在几秒钟内显示我的下一个主菜单屏幕。我在 onBackPressed() 中调用 finish(),我想关闭应用程序在启动屏幕中按后退键。任何人都可以帮助我吗?

谢谢!!

     Thread splashThread = new Thread() {
        @Override
        public void run() {
           try {
              int waited = 0;
              while (_active && (waited < 2000)) {
                 sleep(100);
                 if(_active) {
                     waited += 100;
                 }
              }
           } catch (InterruptedException e) {
              // do nothing
           } finally {

               finish();
               startActivity(new Intent("next activity"));
               stop();
           }
        }
     };
     splashThread.start();

I am creating a splash screen using the following code ,when i press back key the application moves to the home screen and within a few seconds shows my next mainmenu screen.I am calling finish() in onBackPressed(),I want to close the app on pressing back key in the splash screen.can any one help me on this??

Thanks!!

     Thread splashThread = new Thread() {
        @Override
        public void run() {
           try {
              int waited = 0;
              while (_active && (waited < 2000)) {
                 sleep(100);
                 if(_active) {
                     waited += 100;
                 }
              }
           } catch (InterruptedException e) {
              // do nothing
           } finally {

               finish();
               startActivity(new Intent("next activity"));
               stop();
           }
        }
     };
     splashThread.start();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

落日海湾 2024-11-12 13:04:22

这是因为您在 startActivity(new Intent("next Activity")); 之前调用 finish();

finish(); 与 <代码>startActivity(new Intent("下一个活动"));

It's because you call finish(); before the startActivity(new Intent("next activity"));

Swap finish(); with startActivity(new Intent("next activity"));

孤独岁月 2024-11-12 13:04:22

它正在我的应用程序中运行

public class Splash extends Activity {
protected boolean _active = true;
protected int _splashTime = 3000;
Thread splashTread;
private boolean stop = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                while(_active && (waited < _splashTime)) {
                    sleep(100);
                    if(_active) {
                        waited += 100;
                    }
                }

            } catch(InterruptedException e) {
                // do nothing
            } finally {

                if(!stop){
                    startActivity(new Intent(Splash.this,Home.class));
                    finish();
                }
                else
                    finish();
            }
        }

    };
    splashTread.start();

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {

        if(splashTread.isAlive())
            this.stop = true;
    }
    return true;
}

}

It is working in my application

public class Splash extends Activity {
protected boolean _active = true;
protected int _splashTime = 3000;
Thread splashTread;
private boolean stop = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                while(_active && (waited < _splashTime)) {
                    sleep(100);
                    if(_active) {
                        waited += 100;
                    }
                }

            } catch(InterruptedException e) {
                // do nothing
            } finally {

                if(!stop){
                    startActivity(new Intent(Splash.this,Home.class));
                    finish();
                }
                else
                    finish();
            }
        }

    };
    splashTread.start();

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {

        if(splashTread.isAlive())
            this.stop = true;
    }
    return true;
}

}

雪落纷纷 2024-11-12 13:04:22

该解决方案仅解决后退按钮的问题。如果用户按下主页按钮,不良行为仍然会发生。重写 onStop 方法并在那里做你的事情不是更容易吗?

@Override
public void onStop(){
    super.onStop();
    if(splashTread.isAlive())
       this.stop = true;
}

this solution only solves the problem for the back-button. If users press the home-button the unwanted behavior will still occur. Wouldn't it be easier to overwrite the onStop method and do your thing in there?

@Override
public void onStop(){
    super.onStop();
    if(splashTread.isAlive())
       this.stop = true;
}
情愿 2024-11-12 13:04:22

尝试使用这个:

SplashScreen.this.finish();

其中 SplashScreen 是活动的名称。

Try using this:

SplashScreen.this.finish();

where SplashScreen is the name of the Activity.

凉薄对峙 2024-11-12 13:04:22
public class SplashActivity extends Activity {

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

        handler=new Handler();
        handler.postDelayed(() -> {
            Intent intent=new Intent(SplashActivity.this, Home.class);
            startActivity(intent);
            finish();
        },3000);
    }
}

AndriodManifest.xml

    <activity
        android:name=".SplashActivity"
        android:theme="@style/AppTheme"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
public class SplashActivity extends Activity {

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

        handler=new Handler();
        handler.postDelayed(() -> {
            Intent intent=new Intent(SplashActivity.this, Home.class);
            startActivity(intent);
            finish();
        },3000);
    }
}

AndriodManifest.xml

    <activity
        android:name=".SplashActivity"
        android:theme="@style/AppTheme"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文