android intent, intent in android, android put extra,  android start activity, android activity using intent, How to open a new activity using intent in android

How to open a new activity using intent in android

During the development of android applications, you have to find yourself in a scenario where you want to move from one activity to another, that is, suppose you are on the home page of an app like WhatsApp and there is a button for composing messages.

To move from the homepage activity to the new message activity without losing the session of the user who is logged in there is an action performed, this action is made possible through the use of intent in android.

Another example is when you are on the login screen and you have confirmed that the username and password entered by the user are correct, next you will need to redirect the user to the homepage of your app and ensure that you load contents depending on who is logged in, this is made possible again by intents.

An android intent is used to perform actions in android development, for example, you can use intents to start an activity, start a service and send a message between two activities. Like in web development where we use sessions to pass data from one file to another, in android we use intents to ensure that activities communicate with each other by transferring data from one activity to another.

In this article, we shall highlight how to navigate from one activity to another through the use of intents, transfer data from one activity to another through the use of intents, and use different examples of how intents are used in android.

First, to ensure we understand intents well, we will need to have two activities. In this case, we will use an example of a login activity and the main activity that the user is redirected to when the login is complete.

To create these two activities, you can refer to our previous articles on how to create an application in android or a specific one on how to create a login app using kotlin in android. The two articles can guide you to ensure you create activities in android.

Next, we will go straight to the login activity where you confirm that the username and password are correct like shown below

// set on-click listener

        loginbutton.setOnClickListener {

             username = usernametext.text.toString();

             password = passwordtext.text.toString();

 

            if(isEmpty(username) || isEmpty(password)){

                Toast.makeText(this@MainActivity, "Please fill all fields", Toast.LENGTH_LONG).show()

            }else{

                if(username=="username1" && password=="password1") {

                   

                }else{

                    Toast.makeText(this@MainActivity, "Wrong username or password, please try again", Toast.LENGTH_LONG).show() 

                }

            }

        }

In the above code, am assuming that my correct username is username1 and my correct password is password1. If the above is met, that’s where we shall redirect the app to HomeActivity if not, the toast is displayed of username or password being incorrect.

To open a new activity using intent in android, we will use the code snippet below

val intent= Intent(this, HomeActivity::class.java)

startActivity(intent);

To explain the above code,

  • we define a variable that holds the object where at this point inside the intent we define the activity that the activity is directed to
  • "this" represents the current activity
  • The startActivity is the one used to perform the action defined by the intent, that’s why the variable defined above is the one used in this method.

The above code once the login button is clicked and the username and password checked, will open the home activity but a few things will be missing.

In the home activity, we will need to know who logged in and this is made possible by carrying this information while we are opening the activity. We use putExtra to add elements to the intent. In this case, we can transfer the username to the home activity and it will be done as follows

val intent= Intent(this, HomeActivity::class.java)

 intent.putExtra("username",username)

 startActivity(intent);

The element inside the "" is the one that has been assigned the value of username and while we shall be tracing the value it is the one we shall use.

The full code snippet for click action for the login button is as follows

    // set on-click listener

        loginbutton.setOnClickListener {

             username = usernametext.text.toString();

             password = passwordtext.text.toString();

 

            if(isEmpty(username) || isEmpty(password)){

                Toast.makeText(this@MainActivity, "Please fill all fields", Toast.LENGTH_LONG).show()

            }else{

                if(username=="username1" && password=="password1") {

                    val intent= Intent(this, HomeActivity::class.java)

                    intent.putExtra("username",username)

                    startActivity(intent);

                }else{

                    Toast.makeText(this@MainActivity, "Wrong username or password, please try again", Toast.LENGTH_LONG).show()

                }

            }

        }

To get the username in the home activity, you will need to use the following code

val bundle: Bundle? = intent.extras

val name: String? = bundle?.getString("username")

 

That’s all for this article having covered how to use intents and to add elements using put extra. Feel free to test with the same using other examples you may define.