Intent in Android

0

 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 a call then dialer screen is an example of Implicit Intent.

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


Intent  I  = new Intent(PredefineActivity)

call to predefine activity dialog

startActivity(I)


Example of Implicit Intent:-

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>
    


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 Intent(adressofcurrentactivity, NameofAnotherActivity);

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"
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);
}
}
}



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

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)