温馨提示×

打开activity的方法有哪些

小亿
206
2023-09-04 11:04:32
栏目: 编程语言

打开activity的方法有以下几种:

  1. 使用显式Intent打开目标Activity:
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
startActivity(intent);
  1. 使用隐式Intent打开目标Activity:
Intent intent = new Intent();
intent.setAction("com.example.ACTION_TARGET_ACTIVITY");
startActivity(intent);
  1. 使用Intent传递数据到目标Activity:
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
intent.putExtra("key", value);
startActivity(intent);
  1. 使用startActivityForResult()方法打开目标Activity,并获取返回结果:
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
startActivityForResult(intent, REQUEST_CODE);
  1. 使用FLAG_ACTIVITY_NEW_TASK标记打开目标Activity:
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
  1. 使用PendingIntent打开目标Activity:
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(CurrentActivity.this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent.send();

0