5

我一直在开发 Android Native App,我想做的是:

Activities - A -> B -> C  Then  A-> B -> C -> C  . 

从 C Activity 如果它再次指向 C 那么我想手动从堆栈中删除 C 、 B 。在我的背上,它应该只移动到 A 。

我试过完成()但问题是:

Activities - A -> B ->  C  Then  A-> B -> C -> C  on finish A -> B -> C  required state A-> C .

有谁知道如何捕获堆栈中的所有活动并从堆栈中删除特定活动?

4

6 回答 6

6

在活动 C 中,覆盖onBackPressed并添加如下内容:

@Override
public void onBackPressed() {
    if (shouldGoBackToA) {  // There are various ways this could be set
        Intent intent = new Intent(this, AActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    } else {
        finish();
    }
}

FLAG_ACTIVITY_CLEAR_TOP将导致它沿着堆栈向下移动到 A Activity 的现有副本,而不是开始一个新副本。 从文档

public static final int FLAG_ACTIVITY_CLEAR_TOP
如果设置,并且正在启动的 Activity 已经在当前任务中运行,那么不会启动该 Activity 的新实例,而是关闭它之上的所有其他 Activity 并传递此 Intent将(现在在顶部)旧活动作为新意图。

于 2013-11-06T07:37:18.067 回答
0

While calling intent pass a flag called actvity clear top like this:

Intent newIntent=new Intent(this,MainActivity.class);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(newIntent);
于 2013-11-06T07:21:09.573 回答
0

在活动 C 中,当按下后退按钮时,开始活动 A,如下所示:

@Override
public void onBackPressed() {
  Intent intent = new Intent(getApplicationContext(), A.class);
  intent.putExtra("EXIT", true);
  startActivity(intent);
}

然后在活动 A 的 onCreate() 中执行此操作

if (getIntent().getBooleanExtra("EXIT", false)) {
    finish();  
}
于 2013-11-14T23:13:27.713 回答
0

第 1 步:为结果 A -> B -> C1 -> C2 启动活动。使用 startActivityForResult 调用您的活动

Intent intent = new Intent(yourActivity.this, nextActivity.class);
startActivityForResult(intent, 1);

第 2 步:在 C2 中指定您要返回到 A.. 每当您完成活动时,请编写以下代码

Intent i = getIntent();
i.putString("Result","GottoA");
setResult(Activity.RESULT_OK, i);
finish();

第 3 步:每当 C2 完成时,调用 previsus 堆栈 activit 的onActivityResult .. 所以您可以检查 C1 和 B onActivityResult 是否设置了任何结果 bck .. 并相应地完成并在 Activity B 和 c 中实施以下代码

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     Intent i = getIntent();    
     if (resultCode == RESULT_OK && i.getString("Result","null").equals"GottoA") { 
        i.putString("Result","GottoA");
        setResult(RESULT_OK, i);
        finish();
       }
    }
于 2013-11-06T07:32:36.640 回答
0

这个完整的例子可以帮助你......

public class ActivityA extends Activity {

    public static final int ID_TEXTVIEW = 0xDEAF1;
    public static final int ID_BUTTON = 0xDEAF2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View contentView = getContentView(this);
        TextView textView = (TextView) contentView.findViewById(ID_TEXTVIEW);
        textView.setText("ActivityA");
        setContentView(contentView);

        final Button button = (Button) contentView.findViewById(ID_BUTTON);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityA.this, ActivityB.class);
                startActivity(intent);
            }
        });
    }

    public static View getContentView(Context context) {
        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;

        TextView textView = new TextView(context);
        textView.setLayoutParams(layoutParams);
        textView.setId(ID_TEXTVIEW);
        layout.addView(textView);

        Button button = new Button(context);
        button.setText("Next");
        button.setLayoutParams(layoutParams);
        button.setId(ID_BUTTON);
        layout.addView(button);
        return layout;
    }
}



public class ActivityB extends Activity {

    public static final String ACTION_FINISH = "com.myapp.test2.ACTION_FINISH";

    public ActivityB() {
    }

    private FinishReceiver finishReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View contentView = ActivityA.getContentView(this);
        final TextView textView = (TextView) contentView
                .findViewById(ActivityA.ID_TEXTVIEW);
        textView.setText("ActivityB");
        setContentView(contentView);

        final Button button = (Button) contentView
                .findViewById(ActivityA.ID_BUTTON);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityB.this, ActivityC.class);
                startActivity(intent);
            }
        });

        finishReceiver = new FinishReceiver();
        IntentFilter filter = new IntentFilter(ACTION_FINISH);
        registerReceiver(finishReceiver, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(finishReceiver);
    }

    private class FinishReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ACTION_FINISH)) {
                finish();
            }
        }
    }
}


public class ActivityC extends Activity {

    public ActivityC() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View contentView = ActivityA.getContentView(this);
        final TextView textView = (TextView) contentView
                .findViewById(ActivityA.ID_TEXTVIEW);
        textView.setText("ActivityC");
        setContentView(contentView);

        final Button button = (Button) contentView.findViewById(ActivityA.ID_BUTTON);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityB.ACTION_FINISH);
                sendBroadcast(intent);
                intent = new Intent(ActivityC.this, ActivityC.class);
                startActivity(intent);
                finish();
            }
        });
    }
}
于 2013-11-12T13:40:13.603 回答
0

您可以这样使用:在 A 活动中,当传递给 B 活动时,应添加一个像这样的标志 FLAG_ACTIVITY_NO_HISTORY 的意图,

Button b=(Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent newIntent=new Intent(AActivity.this,Bactivty.class);
                newIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(newIntent);
            }
        });

在移动到 CActivity 时:

Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent newIntent = new Intent(Bactivty.this, CActivity.class);
                startActivity(newIntent);
            }
        });

On backpress 现在将带您到 AActivity。

于 2013-11-06T07:52:10.120 回答