public class MainActivity extends AppCompatActivity {
...
// Method to start NewActivity
private void startNewActivity() {
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
}
// Your code that triggers the instruction to start NewActivity
private void onInstructionTriggered() {
// Call the method to start NewActivity
startNewActivity();
}
...
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Call the method to trigger the instruction
onInstructionTriggered();
}
});
这样,当 onInstructionTriggered() 方法被调用时,它将启动 NewActivity 并替换当前启动活动。