Android Intents

Android Intents

A simple tutorial to understand the working of intents

Hammad Maqbool's photo
Hammad Maqbool

Published on Aug 10, 2021

2 min read

Subscribe to my newsletter and never miss my upcoming articles

An Intent in andorid application provides runtime binding between seprate components of android applications.

You can use intents to move from one activity of app to other activity of app, You can use intents to move data from one activity of app to other and you can also call other applications using intent to perform some task for you.

Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(intent); // startActivity allow you to move

Intent to Move from one activity to other with Data

  • message_text is the EditText in this example.

  • string abc is the simple string in this example

  • int a is just an integer in the example

        message_text = (EditText) findViewById(R.id.message_text);  
        String message = message_text.getText().toString();  
        String abc = "This is the string value";
        Int a = 20;
        Intent intent= new Intent(this ,SecondActivity.class);  
        intent.putExtra("MessageNumber1",message);
        intent.putExtra("StringAbc", abc);
        intent.putExtra("intValue", a);  
        startActivity(intent);

How to extract the data in SecondActivity sended by FirstActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);
    //Get the Intent that started this activity and extract the string
    Intent intent = getIntent();
    String message = intent.getStringExtra("MessageNumber1");
    String abc = intent.getStringExtra("StringAbc");
    Int a = intent.getIntExtra("intValue",0);
    //Note the 0 at the end is a default value if the number sent is empty.

Intents with ACTION_SEND When we use the intents with action send we don't know who will recieve the data it is upto user, The user chose the other app where He want to send the data.

Picture below is the example of it.

Untitled.png Here is the code to make this kind of Intent in Java

btn_Share = findViewById(R.id.btn_Share);
TextView_QuoteNumber = findViewById(R.id.tv_QuoteNumber);
textView_Quote = findViewById(R.id.tv_Quote);

final String Quote = textView_Quote.getText().toString();
String QuoteNumber = TextView_QuoteNumber.getText().toString();

btn_Share.setOnClickListener(new View.OnClickListener()
{
            @Override
            public void onClick(View view) {
            Intent sendintent = new Intent();
            sendintent.setAction(Intent.ACTION_SEND);
            sendintent.setType("text/plain");
            sendintent.putExtra(Intent.EXTRA_TEXT, Quote);
            startActivity(sendintent);
 }
   });

The code above is binding some views of the interface into java and on button click putting data from the view into the intent and startig the activity with intent which will initiate the screen as shown above in SS.

 
Share this