您可以尝试使用startActivityForResult()
.
private static final int REQUEST_CODE_MAKE_CLICKABLE = 1;
private boolean mAlreadyClicked = false;
private OnClickListener playClickListener() {
return new OnClickListener() {
@Override
public void onClick(View v) {
if(mAlreadyClicked) return;
mAlreadyClicked = true;
Intent intent = new Intent(getActivity(), TestActivity.class);
startActivityForResult(intent, REQUEST_CODE_MAKE_CLICKABLE );
}
};
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_CODE_MAKE_CLICKABLE && resultCode == Activity.RESULT_OK) {
mAlreadyClicked = false;
}
}
在您的 TestActivity 中,您可以覆盖完成以执行以下操作:
@Override
public void finish() {
setResult(Activity.RESULT_OK, new Intent());
super.finish();
}
这将导致点击只被触发一次,直到下一个活动返回并“重新启用”点击功能。
- 或者 -
您可能会很无聊并保持布尔值并在 onResume() 上简单地“重置”它:
private boolean mAlreadyClicked = false;
private OnClickListener playClickListener() {
return new OnClickListener() {
@Override
public void onClick(View v) {
if(mAlreadyClicked) return;
mAlreadyClicked = true;
Intent intent = new Intent(getActivity(), TestActivity.class);
startActivity(intent, REQUEST_CODE_MAKE_CLICKABLE );
}
};
}
@Override
public void onResume() {
super.onResume();
mAlreadyClicked = false;
}
你应该真的很无聊,但使用跨 Activity 回调总是很有趣。:)