Navigate Activity (Using Intent)
Intent
If you are new to android and want to know what is an Intent and how we can use the intent for navigation in the application then you are at the right place.
What is an Intent?
The intent is an object in android, used to communicate between different components in androids such as activity, services, component, and broadcast receiver.
It also helps to pass the data between different components.
How to navigate from one activity to another activity in android?
Intent mIntent = new Intent(context, yourActivityName.class);
startActivity(mIntent);
In android, we are using the intent to navigate from one screen to another screen. Don't hesitate if you don't know how to use it, let's check it with an example.
Navigation Code:-Intent
If you are new to android and want to know what is an Intent and how we can use the intent for navigation in the application then you are at the right place.
If you are new to android and want to know what is an Intent and how we can use the intent for navigation in the application then you are at the right place.
What is an Intent?
It also helps to pass the data between different components.
How to navigate from one activity to another activity in android?
Intent mIntent = new Intent(context, yourActivityName.class);
startActivity(mIntent);
In android, we are using the intent to navigate from one screen to another screen. Don't hesitate if you don't know how to use it, let's check it with an example.
First, you have to create a to navigate activity.
For creating an activity right on the folder where you want to add the activity and select java class.

Give the appropriate name to that file(Here I m using Second-Activity.java.
Create a Layout file for that java.
For creating a layout right on the res folder, then click on new and select layout resource from it.

Give the appropriate name to that file.
Adding the button to the main activity layout file, on press on the button we are navigating the user to the second activity.
Now add the logic in the button press event of activity main.

Give the appropriate name to that file(Here I m using Second-Activity.java.
Create a Layout file for that java.
For creating a layout right on the res folder, then click on new and select layout resource from it.

Give the appropriate name to that file.
Register your activity to manifest file :-
<?xml version="1.0" encoding="utf-8"?>
<manifest>
<application>
<activity android:name=".SecondActivity"</activity>
</application>
</manifest>
Adding the button to the main activity layout file, on press on the button we are navigating the user to the second activity.
Now add the logic in the button press event of activity main.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button newActivity= (Button) findViewById(R.id.button);
newActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent newact=new Intent(MainActivity.this,SecondActivity.class);
startActivity(newact);
}
});
}
}
Comments
Post a Comment