wisemonkeys logo
FeedNotificationProfileManage Forms
FeedNotificationSearchSign in
wisemonkeys logo

Blogs

Starting Android Activity Using Intent

profile
Fahim
Oct 30, 2017
1 Like
0 Discussions
367 Reads

Intent In Android

Intents basically support you to interact with the  Android parts from the same android applications or with the parts of other applications. For instance, an activity can start an external activity for taking a picture. startActivity() - a method you can use to start an activity.
Bundle. It contains data can be used by the receiving component.
In Android, the reusability of additional application components is known as a task. An application can obtain additional Android components to accomplish a task. For instance, from a component of your application, you can trigger the different component which is already present in the Android system, which handles photos, even if that component is not a portion of your application. In this component, you select a photo and return to your application to use the picked photo.

Starting activities or services

startActivity(intent) - As the name says this method starts the other Activity and this method is defined on the object of whichContext extends.Activity
Intent i = new Intent(this, ActivityB.class);
startActivity(i);

Activities which are originated by separate Android activities are called sub-activities.
startService(Intent)  - As the name says this method starts the other services and is used to start a service via intents.

Sending out Explicit Intent / Implicit intent

explicit intent - when one activity directly targets the other activity in the intent. Explicit Intents specify the component explicitly which should be declared by the Android system, by managing the Java class as the identifier.
how you can build an explicit intent to start the activity.
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("Key1", "This value for SecondActivity");
i.putExtra("Key2", "This value SecondActivity");
 Implicit intents the action which should be performed optionally which provides content for the action. If an implicit intent is sent to the Android, it searches for all components which are registered for the specific action . If several components are identified by the Android system, the user will get a selection dialog and can decide which component should be used for the intent. For Instance, 
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
startActivity(i);

Retrieving result data from a sub-activity

An activity can be closed via the back button on the phone. In this case, the methodfinish() is performed. If you cause the activity with the methodstartActivityForResult() , you await feedback from the subActivity. Once the subActivity finishes, the methodonActivityResult()  is called and you can do actions based on the result. The method startActivityForResult()you can specify a result code which activity you started. The started activity can also set a result code which the caller can use to figure out if the activity was canceled or not. how to trigger an intent with the startActivityForResult()
public void onClick(View view) {
    Intent i = new Intent(this, SecondActivity.class);
    i.putExtra("Value1", "This value one for SecondActivity");
    i.putExtra("Value2", "This value two SecondActivity");
    // set the request code to any code you like,
    // you can identify the callback via this code
    startActivityForResult(i, REQUEST_CODE);
}
If you use the methodstartActivityForResult(), then the begun activity is called a subActivity.
If the subActivity is finished, it can send data back to its caller via an Intent. This is done in the methodfinish().
@Override
public void finish() {
    Intent data = new Intent();
    data.putExtra("Key1", "hey there what is up. ");
    data.putExtra("Key2", "Hope you are having an amazing day. ");
    setResult(RESULT_OK, data);
    super.finish();
}
Once the subActivity finishes, the methodonActivityResult() in the calling activity is called.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
        if (data.hasExtra("Key1")) {
            Toast.makeText(this, data.getExtras().getString("Key1"),
                Toast.LENGTH_SHORT).show();
        }
    }
}

Android Implicit Intent Example

activity_first.xml


<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".FirstActivity" >  
    <EditText  
        android:id="@+id/url"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:ems="10" />  
    <Button  
        android:id="@+id/search"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_below="@+id/url"  
        android:layout_centerHorizontal="true"  
        android:text="Visit" /> 
</RelativeLayout>  
 FirstActivity.java

package in.wisemonkeys.intents;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
final EditText editText1=(EditText)findViewById(R.id.url);
Button button1=(Button)findViewById(R.id.search);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String url=editText1.getText().toString();
Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(intent);
}
});
}
}

Android Explicit Intent

activity_explicit_one.xml

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ExplicitOneActivity" >

<Button
android:id="@+id/intentBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/TextView"
android:layout_marginLeft="65dp"
android:layout_marginTop="38dp"
android:onClick="onClick"
android:text="Call Explicit Two activity" />

<TextView
android:id="@+id/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/intentBtn"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="27dp"
android:minHeight="60dip"
android:text="Explicit One Activity"
android:textSize="20sp" />

</RelativeLayout>
activity_explicit_two.xml

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".ExplicitTwoActivity" >  
  
    <Button  
        android:id="@+id/intentBtn"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_below="@+id/TextView"  
        android:layout_marginLeft="65dp"  
        android:layout_marginTop="38dp"  
        android:onClick="onClick"  
        android:text="Call Explicit one activity" />  
  
    <TextView  
        android:id="@+id/TextView"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/intentBtn"  
        android:layout_alignParentTop="true"  
        android:layout_marginLeft="18dp"  
        android:layout_marginTop="27dp"  
        android:minHeight="60dip"  
        android:text="this is Explicit Two Activity"  
        android:textSize="20sp" /> 
  
</RelativeLayout>  

ExplicitOneActivity.java


package in.wisemonkeys.intents;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class ExplicitOneActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_explicit_one);

Button button1=(Button)findViewById(R.id.intentBtn);

button1.setOnClickListener(new OnClickListener(){

public void onClick(View view) {

Intent i = new Intent(getApplicationContext(), ExplicitTwoActivity.class);

i.putExtra("Value1", "Hello from One");

i.putExtra("Value2", "Hii ExplicitOneActivity");

startActivity(i);

}

});

}

}

ExplicitTwoActivity.java


package in.wisemonkeys.intents;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

public class ExplicitTwoActivity extends Activity {


@Override

public void onCreate(Bundle bundle) {

super.onCreate(bundle);

TextView tv=new TextView(this);

tv.setText("ExplicitTwo activity");

setContentView(R.layout.activity_explicit_two);

Bundle extras = getIntent().getExtras();

String value1 = extras.getString("Value1");

String value2 = extras.getString("Value2");

Button button1=(Button)findViewById(R.id.intentBtn);

button1.setOnClickListener(new OnClickListener(){

public void onClick(View view) {

Intent i = new Intent(getApplicationContext(), ExplicitOneActivity.class);

startActivity(i);

}

});

}

}

Comments ()


Sign in

Read Next

Trends that began during the pandemic

Blog banner

ACHIEVEMENTS IN OPERATING SYSTEMS

Blog banner

How to use GIT & GITHUB

Blog banner

Memory Management

Blog banner

DMZ: Your Secret Weapon for Data Security

Blog banner

Sage

Blog banner

Amazon

Blog banner

Memory Management of Operating System(OS)

Blog banner

Security requirements for Safe E-Payments

Blog banner

CYBERBULLYING: Negative comments!

Blog banner

Raid and levels of raid.

Blog banner

PROCESS STATES OF OPERATING SYSTEM

Blog banner

Information Technology In E- Commerce

Blog banner

Deadlock

Blog banner

THE ROLE OF CYBER FORENSICS IN CRIMINOLOGY

Blog banner

Assignment-3

Blog banner

Electronic Funds Transfer

Blog banner

FRIENDSHIP

Blog banner

Digital marketing spotlight “Dove’s Real Beauty Campaign”

Blog banner

Artificial Intelligence (AI)

Blog banner

What's Better : Supervised or Unsupervised Learning

Blog banner

computer security

Blog banner

Modern Operating Systems.

Blog banner

Throttle engine ’Sneak peek into the future’

Blog banner

Creating Digitally Signed Document

Blog banner

Review on Recovering Deleted Files

Blog banner

Mango Raisin Chutney (sweet mango raisin spread)

Blog banner

Bitcoin sent using radio waves! No internet!

Blog banner

AutoML: The Future of Automated Data Science

Blog banner

Festive Ethnic Wear Guide: Patola-Inspired Looks for Every Celebration

Blog banner

SECURITY VULNERABILITIES COUNTERMEASURES IN A SMART SHIP SYSTEM

Blog banner

Environmental Management using GIS

Blog banner

Constrained Management in IT

Blog banner

E-BUSINESS RISK MANAGEMENT

Blog banner

Telegram and it's features

Blog banner

All you need to know about “On-page SEO”

Blog banner

Jio .....A Revolution ?? ??

Blog banner

Uniprocessor scheduling

Blog banner

Types of OS

Blog banner

10 Reasons to Date a South Indian Girl

Blog banner

Yahoo! mail

Blog banner

How to invest in Indian Stock Market ? ~ Tutorial 1

Blog banner