Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 The intent in Android:-




The intent is created by Intention whose meaning is to navigate from one activity to another. In the android application, we will use the Intent class to manage Intent Activity.



Type of Intent:-


1)  Implicit Intent:-  this type of Intent is used to call predefine activity of android that is already in-built on the android operating system.


For example when we dial call then dialer screen is the example Implicit Intent.

When we surf any URL into the android browser then it will automatically open the browser view that is an example of Implicit Intent.


Intent  I  = new Intent(PredefineActivity)

call to predefine activity dialog

startActivity(I)


Example of Implicit Intent:-


Design File:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".WelcomeActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FirstActivity"
android:id="@+id/btnfirst"
android:onClick="navigateactivity"
></Button>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SecondActivity"
android:id="@+id/btnsecond"
android:onClick="navigateactivity"
></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ThirdActivity"
android:id="@+id/btnthird"
android:onClick="navigateactivity"
></Button>
</LinearLayout>

Java File:-
package com.example.additionapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class WelcomeActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}

public void navigateactivity(View v)
{
if(v.getId()==R.id.btnfirst )
{
Intent i = new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
else if(v.getId()==R.id.btnsecond )
{
Intent i = new Intent(getApplicationContext(),MainActivity2.class);
startActivity(i);
}
else
{
Intent i = new Intent(getApplicationContext(),MainActivity5.class);
startActivity(i);
}
}
}


2)  Explicit Intent:-

this type of Intent is used to implement navigation from one activity to another, for example, if we want to navigate from login activity to dashboard activity then it is an example of Explicit Intent.


Syntax:-


Intent  I = new Inten(adressofcurrentactivity, NameofAnotherActivity);

startActivity(i);



JAVA FILE:-


package com.example.additionapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.net.URI;

public class ImplictIntentActivity extends AppCompatActivity {

Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_implict_intent);
btn = findViewById(R.id.btnurl);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_DIAL);
i.setData(Uri.parse("tel:"+"9893913667"));
startActivity(i);
}
});
}
}

Design File:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ImplictIntentActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OpenURL"
android:id="@+id/btnurl"></Button>

</LinearLayout>
    

How to pass data from One Activity to Another Activity?

We will use Bundle instance to set and get data from Intent.

Android Provide putExtra() to send data using Intent Object, We can pass data using Key=>Value pair where 

Key will be String Type and Value Type can be int,float,string,double,boolean.

Set value on Intent:-

Intent i = new Intent(getApplicationContext(),MainActivity.class);
i.putExtra("rno",1001); //int
i.putExtra("flag",false); //boolean

startActivity(i);


Get value from Intent
Bundle bundle = getIntent().getExtras();
boolean bl = bundle.getBoolean("flag");
int i = bundle.getInt("rno");
if(bl)
{
txtres.setText("RNo is "+i);

}
else
{
txtres.setText("RNO IS NOT SET");
}

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post