从另外一个杀一个活动 [英] Killing one activity from another
问题描述
我有两个活动A和B B是透明穿过活动,并且A看见。我要杀了由$ P $ B pssing一个按钮。
I have two activities A and B. B is a transparent pass through activity, and A is seen. I want to kill B by pressing a button A.
下面是我到目前为止已经试过:
Here's what I've tried so far:
B obj=new B();
obj.finish();
我创建B的一个对象,并试图杀死它。没有工作。
I created an object of B and tried to kill it. That didn't work.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("keep", true);
startActivity(intent);
什么是应该这样code做的是明确的最上面的活动,这是B和再次调用B,但这次我传递一个值,使得B中的几秒钟后,会杀死自己。
What this code is supposed to do is clear the top most activity, which is B and call B again, except this time I'm passing a value such that B kills itself after a few seconds.
此只堆积由于某种原因,活动的多个实例。或者至少我认为这是因为屏幕变得像素化,由于许多透明的活动所发生的事情。
This only piled up more instances of the activity for some reason. Or at least I think that's what happened because the screen became pixelated due to many transparent activities.
下面是我的清单:
<activity
android:name="com.xxx.xxx.B"
android:excludeFromRecents="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:clearTaskOnLaunch="true" >
</activity>
我有什么做的,这样,当我按下按钮后会显示活动和第二次杀死呢?创作的部分显然是照顾。我b活动弹出,我想现在杀它的B是在顶部。
What do I have to do so that, when I hit a button once the activity is displayed and the second time kills it? The creation part is obviously taken care of. My activity B pops up, I want to kill it now that B is on top.
修改
我想这有一个复选框,这里的code:
I tried this with a checkBox, here's the code:
enable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finishActivity(0);
Intent intent = new Intent(A.this, B.class);
if (enable.isChecked()) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("keep", true);
intent.putExtra("value", 10);
startActivityForResult(intent, 0);
}
else
{
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("keep", false);
startActivityForResult(intent, 0);
}
}
});
在使被检查活动被调用时,工作正常。但他们继续打桩。它不喜欢这个A-> B-> A-> B当我检查,并取消选中该复选框。这是A-> B-> BB-> BBB
When enable is checked the activity is called, which works fine. But they keep piling on. It's not like this A->B->A->B when I check and uncheck the checkBox. It's A->B->BB->BBB
推荐答案
您可以尝试通过调用该活动的静态方法直接杀死一个活动:
You could try to directly kill an activity by calling a static method in that activity:
活动A应具有可变
static ActivityA activityA;
在的onCreate状态:
In onCreate state:
activityA = this;
和添加这个方法:
public static ActivityA getInstance(){
return activityA;
}
在活动B,调用函数的恒等式的getInstance()
In activity B, call the fuction getInstance()
ActivityA.getInstance().finish();
这篇关于从另外一个杀一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!