![](/img/trans.png)
[英]Is it possible that after launching new activity the previous activity keeps sending data to new activity via intent?
[英]Intent activity keeps popping
我为这个应用程序设置了一个计时器,所以当计时器结束并且 Itemdata 中的框仍然被选中时,它应该调用Thanks.class 活动。 但是,我意识到它对Thanks.class 活动进行了多次分类。 例如,如果有 3 个选定的项目,它会调用Thanks.class 活动 3 次。
这是我声明的计时器
//Declare timer
CountDownTimer cTimer = null;
//start timer function
void startTimer() {
cTimer = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Intent intent = new Intent(ctx, Thanks.class);
ctx.startActivity(intent);
}
};
cTimer.start();
}
//cancel timer
void cancelTimer() {
if(cTimer!=null)
cTimer.cancel();
}
这是我调用函数的方式。
@Override
public View getView(final int position, final View view, ViewGroup parent) {
final GridItemData itemData = getItem(position);
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View itemView = inflater.inflate(R.layout.grid_image, parent, false);
ivGallery = itemView.findViewById(R.id.grid_item_image);
ivGallery.setImageResource(getItem(position).imageUrl);
final MainActivity main = new MainActivity();
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ivGallery = itemView.findViewById(R.id.grid_item_image);
itemData.setSelected(!itemData.isSelected());
startTimer();
if(itemData == getItem(8)) {
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
customizeAlert(builder, v);
builder.show();
}
else if (itemData == getItem(0)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.clickfloor: R.drawable.floor);
}
else if (itemData == getItem(1)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.cb: R.drawable.wb);
}
else if (itemData == getItem(2)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.cs: R.drawable.s);
}
else if (itemData == getItem(3)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.clickothers: R.drawable.others);
}
}
});
return itemView;
}
请指教我该怎么办,谢谢!
只启动一次CountDownTimer
怎么样?
void startTimer() {
if(cTimer == null) {
cTimer = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Intent intent = new Intent(ctx, Thanks.class);
ctx.startActivity(intent);
}
};
cTimer.start();
}
}
或者,当第二个即将启动时,停止第一个CountDownTimer
。
那是因为您为每次点击启动了一个 CountdownTimer,但您没有检查是否已经有一个需要取消或重新启动的 CountDownTimer 活动。
如果您将此扩展到生产,我建议为此使用 AsyncTasks 或 Courotines,因为当您的 CountDownTimer 完成后,您可能不再允许更改 UI-State,并且您将收到 IllegalStateException 或您的 findByView 将抛出 NullPointerException。 使用 AsyncTasks 和 Courotines,您也无需担心生成 CountDownTimer 的多个实例。https://developer.android.com/reference/android/os/AsyncTask
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.