启动,关闭Activity的两种方法

时间:2022-03-11 17:40:41

1. startActivity(Intent intent): 启动其他Activity

  对应关闭方法finish():结束当前Activity

 1 public class MainActivity extends AppCompatActivity
 2 {
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState)
 5     {
 6         super.onCreate(savedInstanceState);
 7         setContentView(R.layout.activity_main);
 8         // 获取应用程序中的bn按钮
 9         Button bn = (Button) findViewById(R.id.bn);
10         // 为bn按钮绑定事件监听器
11         bn.setOnClickListener(new View.OnClickListener()
12         {
13             @Override
14             public void onClick(View source)
15             {
16                 // 创建需要启动的Activity对应的Intent
17                 Intent intent = new Intent(MainActivity.this,
18                         SecondActivity.class);
19                 // 启动intent对应的Activity
20                 startActivity(intent);
21             }
22         });
23     }
24 }

2. startActivityForResult(Intent intent, int requestCode)

  对应关闭方法: finishActivity(int requestCode) 结束以 startActivityForResult(Intent intent, int requestCode)方法启动的Activity