当上下文被破坏时,我们不能通过使用意图来启动新的 Activity 吗?我对吗?

We cannot start a new Activity by using an intent when context has been destroy? Am I right?

当上下文被破坏时,我们不能通过使用意图来启动一个新的Activity?我对吗? 以及为什么在 Thread 为 运行?

时按下后退按钮时我可以启动 activity

下面是我的代码

public class Main3Activity extends AppCompatActivity {

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

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Intent intent = new Intent(this, Main2Activity.class);
        startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}

是的,您无法启动 activity,因为线程休眠时 onBackPressed() 操作会触发 InterruptedException。

无论如何要启动 activity,您可以像这样在 catch 块中移动意图。

public class Main3Activity extends AppCompatActivity {

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

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
        Intent intent = new Intent(this, Main2Activity.class);
        startActivity(intent);
    }

}

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}

}