Start new activity android
To start a new activity in Android, you can use the Intent
class to create an intent that specifies the activity you want to start, and then use the startActivity()
method to start the activity.
Here is an example of how to start a new activity:
Intent intent = new Intent(this, MyNewActivity.class);
startActivity(intent);
In this example, this
refers to the current activity, and MyNewActivity
is the name of the new activity you want to start.
You can also pass data from the current activity to the new activity by adding extras to the intent:
Intent intent = new Intent(this, MyNewActivity.class);
intent.putExtra("key", "value");
startActivity(intent);
In this example, the putExtra()
method is used to add a key-value pair to the intent. The key is "key" and the value is "value".
You can also use the startActivityForResult()
method to start a new activity and receive a result back from the new activity:
Intent intent = new Intent(this, MyNewActivity.class);
startActivityForResult(intent, REQUEST_CODE);
In this example, REQUEST_CODE
is a unique integer that identifies the request to start the new activity. When the new activity finishes, the onActivityResult()
method will be called in the current activity, and you can retrieve the result using the getResult()
method:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
String result = data.getStringExtra("result");
// do something with the result
}
}
}
In this example, the onActivityResult()
method is called when the new activity finishes, and the result is retrieved using the getStringExtra()
method.
You can also use the startActivityFromFragment()
method to start a new activity from a fragment:
Intent intent = new Intent(this, MyNewActivity.class);
startActivityFromFragment(this, intent, REQUEST_CODE);
In this example, this
refers to the fragment, and REQUEST_CODE
is a unique integer that identifies the request to start the new activity.
You can also use the startActivityForResultFromFragment()
method to start a new activity from a fragment and receive a result back:
Intent intent = new Intent(this, MyNewActivity.class);
startActivityForResultFromFragment(this, intent, REQUEST_CODE);
In this example, this
refers to the fragment, and REQUEST_CODE
is a unique integer that identifies the request to start the new activity. When the new activity finishes, the onActivityResult()
method will be called in the fragment, and you can retrieve the result using the getResult()
method:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
String result = data.getStringExtra("result");
// do something with the result
}
}
}
In this example, the onActivityResult()
method is called when the new activity finishes, and the result is retrieved using the getStringExtra()
method.
Note that you need to declare the new activity in the AndroidManifest.xml file:
<activity
android:name=".MyNewActivity"
android:label="@string/app_name">
</activity>
This is just a basic example, and you can customize the intent and the activity as needed.