使用Intent时如何销毁和创建新的活动实例

时间:2016-10-03 06:42:13

标签: android android-intent android-activity

我的活动流程 A-> B-> C 。现在,当我点击活动CI中的按钮时,想要通过销毁活动B的现有实例并创建活动B的新实例来打开活动B.再次,如果我从活动C按下后退按钮,那么它应该打开活动B的现有实例。

我试过的代码是:

    Intent i=new Intent(C.this,B.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);

因此,在执行此意图堆栈后,应该 A-> B 。 但是这并没有创建活动B的新实例。

2 个答案:

答案 0 :(得分:2)

你能做的是

    来自B活动的
  1. startActivityForResult()
  2. 然后在按下次按钮上的活动c setResult() 确定中完成活动&在onBackpressed() setResult()失败
  3. 如果您获得结果确定,则在B中onActivityResult()完成活动,如果您发送结果失败,则在onActivityResult()
  4. 中不执行任何操作
  5. 如果您设置结果OK,则从活动C开始使用新鲜实例
  6. 启动活动b

答案 1 :(得分:1)

启动活动C

ActivityB

public class ActivityB extends AppCompatActivity implements View.OnClickListener {

private TextView activity;
private Button start_next_activity;
private final int req_code_reset_activity = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();

    activity.setText("ActivityB");
    start_next_activity.setText("Start Activity C");


    Bundle res = getIntent().getExtras();
    if (res != null) {
        String my_data = res.getString("my_data");
        if (my_data != null)
            activity.setText(activity.getText() + " data rcvd = " + my_data);
    }

}


private void initView() {
    activity = (TextView) findViewById(R.id.activity);
    start_next_activity = (Button) findViewById(R.id.start_next_activity);

    start_next_activity.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.start_next_activity:
            startActivityForResult(new Intent(this, ActivityC.class), req_code_reset_activity);
            break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    if (requestCode == req_code_reset_activity && resultCode == RESULT_OK) {

        if (null != data) {
            Bundle res = data.getExtras();
            String result = res.getString("my_data");
            Log.d("my_data", "my_data:" + result);
            restartActivity(data);

        }
    }


}

public void restartActivity(Intent intent) {
    Intent mIntent = getIntent();
    mIntent.putExtras(intent.getExtras());
    finish();
    startActivity(mIntent);
}
}

启动活动C,它会将结果返回到活动B需要重新创建的天气。

按下后退按钮时,将恢复ActivityB,当您单击按钮时,将重新创建活动B。

ActivityC

public class ActivityC extends AppCompatActivity implements View.OnClickListener {
private TextView activity;
private Button start_next_activity;
private final int req_code_reset_activity = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();

    activity.setText("ActivityC");
    start_next_activity.setText("New instance Activity B");

}


private void initView() {
    activity = (TextView) findViewById(R.id.activity);
    start_next_activity = (Button) findViewById(R.id.start_next_activity);
    start_next_activity.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.start_next_activity:
            Bundle conData = new Bundle();
            conData.putString("my_data", "This is sample data");
            Intent intent = new Intent();
            intent.putExtras(conData);
            setResult(RESULT_OK, intent);
            finish();
            break;
    }
}
}