我有一堆活动,如A-> B-> C-> D ...所有活动都启用了后退功能。现在关于活动D的具体行动,我想转到活动B,即D& C现在完成,活动B恢复。如何实现这种转变?
答案 0 :(得分:1)
从活动D中,在Intent中使用适当的标志调用活动B. Intent.FLAG_ACTIVITY_CLEAR_TOP应该这样做。
示例代码:
Intent intent = new Intent(this, B.class)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();
答案 1 :(得分:0)
尝试在D
活动
C close = new C();
close.finish();
D.this.finish();
在清单文件的活动标签中添加此属性
android:launchmode="singleTop" //Or singleTask
这样你就可以关闭c和D活动并恢复到B。
答案 2 :(得分:0)
为了澄清,请使用此API< 11:
Intent intent= new Intent(this, Login2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(Intent);
在API级别11中,为此添加了一个新的Intent Flag:Intent.FLAG_ACTIVITY_CLEAR_TASK
Intent intent= new Intent(this, Login2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(Intent);
答案 3 :(得分:0)
如果您想要遵循正确和理想的流程,请转到" onActivityResult"方法。假设你的例子当B转到C时,那时它将使用startActivityForResult启动活动C,当C转到D时,C再次使用statActivityForResult去做D.
对于您的情况,
public class B extends Activity
{
public static int RESULT_CODE=111;
public void onCreate(Bundle SavedInstance)
{
...
....
Intent intent = new Intent(B.this,C.class);
startActivityForResult(intent,REQUEST_CODE);
}
public void onActivityResult(int requestCode,int resultCode,Intent data)
super.onActivityResult(requestCode, resultCode, data);
if(requestcode == C.RESULT_OK)
{
//do you work when D is finished and comes back to B
}
}
public class C extends Activity
{
public static int RESULT_CODE=222;
public void onCreate(Bundle SavedInstance)
{
...
....
Intent intent = new Intent(C.this,D.class);
startActivityForResult(intent,REQUEST_CODE);
}
public void onActivityResult(int requestCode,int resultCode,Intent data)
super.onActivityResult(requestCode, resultCode, data);
if(requestcode == D.RESULT_OK)
{
setResult(RESULT_OK);
}
}
public class D extends Activity
{
public static int RESULT_CODE=333;
public void onCreate(Bundle SavedInstance)
{
...
....
}
//your back click function
public void onBackClick(View v)
{
setResult(RESULT_OK);
}