如何防止Android Oreo中的dobleTap启动重复活动

时间:2017-09-27 11:26:50

标签: java android android-activity startactivityforresult android-8.0-oreo

我知道有很多相同的问题但我找不到Android Oreo(8.0,API 26)的解决方案。

我的问题是什么:

如果我点按了发送 activityForResult 意图的按钮,则活动会开始更多次!

奇怪的事情:

  1. 经典'startActivity(intent)'不执行此操作“问题”=活动仅启动一次

  2. 仅在使用Android Oreo的设备/模拟器上发生(旧版本可以正常工作)

  3. 示例代码:

    清单:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="sandbox.jarda.cz.activityresulttest">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <activity
                android:name=".SecondActivity"
                android:launchMode="singleTop" />
    
        </application>
    
    </manifest>
    

    Java代码示例:

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ((Button) findViewById(R.id.startButton)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
    
                    // starts Activity more times on double tap
                    startActivityForResult(intent, 0);
    
                    //works fine BUT I WANT RESULT
                    startActivity(intent);
                }
            });
        }
    }
    

3 个答案:

答案 0 :(得分:1)

尝试禁用视图,然后在启动活动后启用

@Override
public void onClick(View view) {
     view.setEnabled(false);   
     Intent intent = new Intent(getApplicationContext(), SecondActivity.class);

     // starts Activity more times on double tap
     startActivityForResult(intent, 0);

     //enable button
     view.setEnabled(true);

     //OR enable button after half second etc
     new Handler().postDelayed(new Runnable() {
        view.setEnabled(true); 
     }, 500);

 }

此外,您可以保持禁用状态,直到您从其他活动中恢复。在MainActivity的onActivityResult中启用按钮

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    //enable button here
}

答案 1 :(得分:1)

单击按钮后,您可以禁用该按钮,如下所示。

Button.setEnabled(false);

您可以重写以下代码

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((Button) findViewById(R.id.startButton)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
         ((Button) findViewById(R.id.startButton)).setEnabled(false);
                Intent intent = new Intent(getApplicationContext(), SecondActivity.class);

                // starts Activity more times on double tap
                startActivityForResult(intent, 0);

                //works fine BUT I WANT RESULT
                startActivity(intent);
            }
        });
    }
}

答案 2 :(得分:1)

您可以使用此按钮禁用该按钮几毫秒。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ((Button) findViewById(R.id.startButton)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            view.setEnabled(false);
            view.postDelayed(() -> view.setEnabled(true),200);
            Intent intent = new Intent(getApplicationContext(), SecondActivity.class);

            // starts Activity more times on double tap
            startActivityForResult(intent, 0);

            //works fine BUT I WANT RESULT
            startActivity(intent);
        }
    });
}
}