Android new activity with back button
To create a new activity with a back button in Android, you can follow these steps:
Step 1: Create a new activity
In your Android project, create a new Java class that extends the Activity
class. For example, let's call it MyNewActivity.java
.
Step 2: Define the activity in the AndroidManifest.xml file
In the AndroidManifest.xml
file, add a new <activity>
element to define the new activity:
<activity
android:name=".MyNewActivity"
android:label="@string/title_new_activity">
</activity>
Step 3: Create the activity layout
Create a new layout file for the new activity, for example activity_my_new_activity.xml
. This file will define the UI elements for the activity.
Step 4: Set the activity layout in the activity class
In the MyNewActivity.java
file, set the layout for the activity using the setContentView()
method:
public class MyNewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_new_activity);
}
}
Step 5: Add a back button
To add a back button to the activity, you can use the ActionBar
or a custom button. Here, we'll use the ActionBar
:
public class MyNewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_new_activity);
// Set the action bar
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
Step 6: Handle the back button click
To handle the back button click, you need to override the onOptionsItemSelected()
method:
public class MyNewActivity extends AppCompatActivity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// Handle the back button click
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
In this example, when the back button is clicked, the finish()
method is called to close the activity.
Step 7: Start the new activity
To start the new activity, you can use the Intent
class:
Intent intent = new Intent(this, MyNewActivity.class);
startActivity(intent);
That's it! You should now have a new activity with a back button that closes the activity when clicked.
Here's the complete code:
public class MyNewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_new_activity);
// Set the action bar
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// Handle the back button click
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
And the AndroidManifest.xml
file:
<activity
android:name=".MyNewActivity"
android:label="@string/title_new_activity">
</activity>
Note: Make sure to replace @string/title_new_activity
with the actual string resource for the title of your new activity.