Showing posts with label data pass between activity. Show all posts
Showing posts with label data pass between activity. Show all posts

Friday, December 23, 2016

Intent, Explicit Intent

Intent is a communication medium between components. Like Activities, Services, Receivers and internal components like contact list, camera etc. 

When a intent is created with directly define target component then it is called Explicit Intent.

 
 
 
 
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("name", "Call By Explicit Intent.");
startActivity(intent);




Steps to use Explicit Intent in your project.
  1. Crate a project in android studio.
  2.  Open your main_activity.xml and update it-
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="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="com.pankaj.explicitintent.demo.MainActivity">
    
        <Button
            android:layout_width="match_parent"
            android:text="Explicit Intent"
            android:onClick="openSecondActivity"
            android:layout_height="wrap_content" />
    
    </RelativeLayout>
    
      
  3. Open your MainActivity.JAVA class and update it for change text at run time-

    package com.pankaj.explicitintent.demo;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        //___________methode will be call when click on "Explicit Intent"
        public void openSecondActivity(View view) {
    
            //__________create intent to open SecondActivity.
            Intent intent = new Intent(this, SecondActivity.class);
    
            //__________put some data for sending to second activity.
            intent.putExtra("name", "Call By Explicit Intent.");
    
            //____________call to start the second activity.
            startActivity(intent);
        }
    }
    
    

  4. Create an activity(MainActivity.java) which will be open by explicit intent. Open your MainActivity.java and update it-


    package com.pankaj.explicitintent.demo;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.Toast;
    
    public class SecondActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            //____________get data which is sent from mainactivity by intent
            String name = getIntent().getStringExtra("name");
    
            //____________Show this data into the toast.
            Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
        }
    }
    

  5. Open your activity_second.xml and update it-


    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="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="com.pankaj.explicitintent.demo.SecondActivity">
    
        <TextView
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="second activity" />
    
    </RelativeLayout>
    
    


     
  6. Now all the development process for Explicit Intent has completed, Please run the application.


     
  7. Click on "Explicit Intent".
     



  8.  Good bye, Thanks to read this blog.