简体   繁体   English

Android意图多次执行另一个活动

[英]Android intent to another activity being executed multiple times

I have an intent that when the user touches anywhere on the splashscreen activity they're directed to the gallery activity, this seemingly works fine for the most part and when running the app does as is expected until the back button is pressed. 我的意图是,当用户在启动画面活动中的任何地方触摸时,将他们定向到图库活动,这似乎在大多数情况下都可以正常工作,并且在运行应用程序时按预期运行,直到按下后退按钮。 When pressed multiple presses are required to return to the splashscreen. 当按下时,需要多次按下才能返回到初始屏幕。

Using logcat I was able to find out that the intent is being run multiple times however I'm unable to understand why, here's the method for the intent. 使用logcat,我能够发现该意图被多次运行,但是我无法理解为什么,这是意图的方法。

private void FullScreenOnTouchEvent() {

    LinearLayout layout = (LinearLayout) findViewById(R.id.activity_splashscreen_layout);

    layout.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            startActivity(new Intent(Splashscreen.this, Gallery.class));
            Log.d("Splashscreen", "splashscreen executed");
            return true;            
        }

    });
}

I placed a check when the intent was executed and the new activity (gallery) had opened, the messages displayed are "splashscreen executed" and "gallery executed" respectively. 当执行意图并打开新活动(图库)时,我进行了检查,显示的消息分别是“闪屏执行”和“图库执行”。 Here's a copy of the logcat. 这是logcat的副本。

splashscreen executed / Gallery has executed / splashscreen executed / Gallery has executed / 启动屏幕已执行/库已执行/启动屏幕已执行/库已执行/

I'm unable to see why this is ocurring and i'm at a bit of a deadend research wise, any help would be much appreciated. 我看不到为什么会发生这种情况,而且我的研究还有些死板,任何帮助将不胜感激。

Thank you, Damon. 谢谢你,达蒙

onTouch() is being called multiple times since it recognizes touch inputs of many kinds (DOWN, UP, MOVE...). 由于onTouch()可以识别多种触摸输入(DOWN,UP,MOVE ...),因此被多次调用。 You can fix this by an example written HERE or change layout listener to the OnClickListener which would be more simple to implement (less code). 您可以通过在此处编写示例来解决此问题,或者将布局侦听器更改为OnClickListener ,这将更易于实现(更少的代码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM