简体   繁体   English

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

[英]How to destroy and create new instance of activity when using Intent

I have activity flow A->B->C . 我有活动流程A-> B-> C. Now when I click on button in Activity CI want to open activity B by destroying existing instance of Activity B and creating new instance of Activity B. Again if from Activity C if I press on back button then it should open existing instance of Activity B. 现在,当我点击活动CI中的按钮时,想要通过销毁活动B的现有实例并创建活动B的新实例来打开活动B.再次,如果我从活动C按下后退按钮,那么它应该打开活动B的现有实例。

Code I tried is : 我试过的代码是:

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

So after executing this intent stack should be A->B . 因此在执行此意图堆栈后应该是A-> B. But this doesn't creating new instance of Activity B. 但这不会创建活动B的新实例。

What you can do is 你能做的是

  1. startActivityForResult() from B activity 来自B活动的startActivityForResult()
  2. Then in Acitivity c setResult() OK on button click finish the activity & in onBackpressed() setResult() to fail 然后在Acitivity c setResult() OK按钮上单击完成活动并在onBackpressed() setResult()失败
  3. If you get result OK then finish activity in onActivityResult() in B , If you sent result fail then do nothing in onActivityResult() 如果你得到结果OK然后在B中的onActivityResult()中完成活动,如果你发送结果失败,那么在onActivityResult()什么也不做
  4. If you set result OK then start activity b from activity C with fresh instnace 如果设置结果OK,则从活动C开始使用新的instnace活动b

Start Activity C 开始活动C.

ActivityB 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);
}
}

Start Activity C, it will return a result to Activity B weather it needs to re-create or not. 启动Activity C,它会将结果返回给Activity B,它需要重新创建或不重新创建。

when back button is pressed ActivityB will be restored and when you click on button new instance Activity B will be recreated. 当按下后退按钮时,将恢复ActivityB,当您单击按钮时,将重新创建新实例活动B.

ActivityC 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;
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当上下文被破坏时,我们不能通过使用意图来启动新的活动吗? 我对吗? - We cannot start a new Activity by using an intent when context has been destroy? Am I right? 通过意图切换到另一个活动时如何销毁一个活动? - How to destroy an activity when switching to another activity via intent? 使用FLAG_ACTIVITY_CLEAR_TOP时如何强制Intent不重新创建Activity实例 - How force Intent not to re-create Activity instance while using FLAG_ACTIVITY_CLEAR_TOP 如何使用SherlockListFragment意图启动新活动? - How To Start New Activity with Intent Using SherlockListFragment? 使用Intent启动新活动时,发生“找不到活动”异常 - Activity not found exception occurs when new activity is started using Intent Android:使用Intent启动新活动时找不到活动 - Android: Activity not found when starting new activity using intent 使用new而不是intent在另一个内部创建活动是否符合犹太标准? - Is it kosher to create an activity inside another using new instead of an intent? Android:Intent Flag用于销毁活动并启动新活动 - Android: Intent Flag to destroy activity and start new one Android:创建意图/活动的新实例 - Android: Creating a new instance of an intent/activity 使用意图启动新活动时布局未显示 - Layout not showing up when using intent to start new activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM