在Android中,您可以通過創建自定義的Intent來啟動不同的Activity
Intent customIntent = new Intent(this, CustomActivity.class);
這里,this表示當前Activity的上下文(Context),CustomActivity.class是您要啟動的目標Activity。
如果您想在啟動Activity時傳遞一些額外的信息,可以使用putExtra()方法。例如,傳遞一個字符串和一個整數:
customIntent.putExtra("key_string", "Hello World");
customIntent.putExtra("key_int", 42);
使用startActivity()方法啟動自定義Intent:
startActivity(customIntent);
在目標Activity(在本例中為CustomActivity)中,您可以使用getIntent()方法獲取傳遞的Intent,然后使用getStringExtra()和getIntExtra()等方法獲取額外的數據:
Intent intent = getIntent();
String stringValue = intent.getStringExtra("key_string");
int intValue = intent.getIntExtra("key_int", defaultValue);
這里的defaultValue是在獲取整數時使用的默認值,如果傳遞的值不存在,則返回此默認值。
通過以上步驟,您可以在Android中創建和使用自定義Intent來啟動不同的Activity。