2015-11-23 90 views
4

我有一个活动的层次和我有一个按钮,让我从一个活动d到活动B.管理android的堆栈中

image

的事情是,会从d到B会离开C对所以如果我做A-> C-> D-> B,然后按回它会把我送到C,而不是A(这是我想要的)。

当我点击B按钮或者是否有某种解决方法时,是否有删除C的方法?

+0

您可以用片段做到这一点,而不是,FragmentManager让您管理堆栈。同时查看父级活动:http://developer.android.com/training/implementing-navigation/temporal.html –

回答

0

考虑使用A作为调度程序。当你想从D推出B并在此过程完成C,为此在D

// Launch A (our dispatcher) 
Intent intent = new Intent(this, A.class); 
// Setting CLEAR_TOP ensures that all other activities on top of A will be finished 
// and setting SINGLE_TOP ensures that a new instance of A will not 
// be created (the existing instance will be reused and onNewIntent() will be called) 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); 
// Add an extra telling A that it should launch B 
intent.putExtra("startB", true); 
startActivity(intent); 

A.onNewIntent()做到这一点:

@Override 
protected void onNewIntent(Intent intent) { 
    if (intent.hasExtra("startB")) { 
     // Need to start B from here 
     startActivity(new Intent(this, B.class)); 
    } 
} 
+0

谢谢,工作得很好。这种方法如何工作效率明智?在运行intent之前它是否一直运行到onResume()? –

+1

可能。在这种情况下我不记得它是否调用了'onResume()',因为另一个Activity将被启动。它可能。你可以添加一些日志来看看你自己。但我认为这不是一个值得担心的效率问题。 –

0

我不知道你打电话给B,C和D的具体细节,或者是什么数据传过来的,但是如果你愿意,你可以打电话给D来关闭C。

在C,开始d时,你可以这样做:

Intent intent = new Intent(this, D.class); 
startActivity(intent); 
finish(); 

终点开始D.

后再次将关闭C,没有太多的信息,这只是在黑暗中拍摄,虽然。

+0

A,B和C可以通过按钮栏从任何其他活动中调用,而D只能从C. 我不想完成C,因为我打开D,因为按下后退按钮会让我回到A. –

+0

这种方法是行不通的。如果你想从D转到B,那么你可能需要使用带有片段管理器的片段,或者创建自己的堆栈管理器。 – TooManyEduardos