如何防止在Android Oreo中的Dobletap上开始双重活动



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

我的问题是什么:

当我在按钮上点击更多时间以启动 activityforresult 时,活动开始了更多时间!

奇怪的东西:

  1. classic'startActivity(意图)'不要这样做"问题" =活动仅启动一次

  2. 它仅发生在带有Android Oreo的设备/模拟器上(较旧版本正常工作)

示例代码:

清单:

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

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

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

您也可以继续禁用,直到从其他活动中恢复过来为止。启用Main Activitive on Activitive

中的按钮
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //enable button here
}

单击下面的单击后,您可以禁用该按钮。

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

您可以通过使用此按钮禁用该按钮。

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

相关内容

  • 没有找到相关文章

最新更新