Intent

It's asynchronous message among components of application or with other application. Component of application includes Activity, Service and BroadcastReceiver. Android application components(Activity,Services,BroadcastReceiver) can connect other components or to other Android applications. Intents is a built in class of the android.content.Intenttype. We can send them to the Android system defining the components you are targeting for example Android application components(Activity,Services,BroadcastReceiver) or other application.
Intents are asynchronous messages which allow application components to request functionality from other Android components. Intents allow you to interact with components from the same applications (Internal Intent) as well as with components contributed by other applications(External Intent). For example, send message or move between two activities of same application or an activity can start an external activity for taking a picture. An intent can contain data via a Bundle and can be used by the receiving component. From a application component we can call component in the Android system(Other Application like camera etc.) In this android component you can take a photo by camera application from your intent to it and then came back to application to use the captured photo.


Start Activity/Service from Activity/Service

Create two activities (Java classes, layouts, Bind it and register it in androidmanifest.xml
Intent i = new Intent(this, ActivityTwo.class);startActivity(i);

Sending explicit intents

Create two activities (Java classes, layouts, Bind it and register it in androidmanifest.xml)
In first activity code this
Intent i = new Intent(this, ActivityTwo.class);i.putExtra("msg", "message one");startActivity(i);
In the landing activity where you will to go, here you can the get the Message from
String message = getIntent().getExtras().getString("message");

Sending implicit Intent

To open link in the  browser(Other application).
Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.thecodeexpo.blogspot.com")); startActivity(i);

File Name
Code
XML Layout
activity_main.xml
<?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=".MainActivity">

  <Button
      android:id="@+id/send_intent"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hello World!" />
  <EditText
      android:id="@+id/input"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="#d3d3d3"
      android:hint="Enter here" />

</LinearLayout>
Java Code
MainActivity.java
public class MainActivity extends AppCompatActivity {
  Button btnTest;
  EditText textTest;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      btnTest=findViewById(R.id.send_intent);
      textTest=findViewById(R.id.input);
      btnTest.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              //Code Here
              Intent intent=new Intent(MainActivity.this,SecondActivity.class);
              intent.putExtra("input","Huma was sleeping");
              intent.putExtra("check",true);
              intent.putExtra("numbers",345);
              intent.putExtra("userinput",textTest.getText().toString());
              startActivity(intent);
          }
      });
  }
}
XML Layout
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
      android:id="@+id/msg"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</LinearLayout>
Java Code
SecondActivity.java
public class SecondActivity extends Activity {
  TextView msg;
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.second_layout);
      msg=findViewById(R.id.msg);
      Intent intent=getIntent();
      String str=intent.getStringExtra("input");
      boolean checkValue=intent.getBooleanExtra("check",false);
      int numbersValue=intent.getIntExtra("numbers",0);
      String userInput=intent.getStringExtra("userinput");
      msg.setText(str+checkValue+numbersValue+userInput);
  }

  @Override
  protected void onDestroy() {
      super.onDestroy();
  }

  @Override
  protected void onPause() {
      super.onPause();
  }

  @Override
  protected void onResume() {
      super.onResume();
  }
}
Task
1. Login activity(username edittext,password edittext,login button, forgot password textview)
2. Profile activity(name, father name etc.)
3. Forgot password activity (email edittext, send mail button)
4. login button click --> Profile activity
5. forgot password textview click --> Forgot password activity

Comments