Subscribe Now

* You will receive the latest news and updates on your favorite celebrities!

Trending News

Blog Post

How to start one activity from another in android using Intent?
Beginner

How to start one activity from another in android using Intent?

Today we are going to learn about Android Intents. Just imagine if you want to start one screen from another screen then you have to use Intent. Even if you want to pass some data from one activity to another then also Intent is helpful. So the first question in your mind will be

What is Android Intent?

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an ActivitybroadcastIntent to send it to any interested BroadcastReceiver components, and Context.startService(Intent) or Context.bindService(Intent, ServiceConnection, int) to communicate with a background Service.

Pass data from one activity to another using intent

 

What are the types of Intent?

  • Explicit Intent:

Explicit intent is used to connect the internal world of application, Suppose you want to connect one activity to another we can do this by Explicit Intent.

// explicit intent by specifying its class name
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);
  • Implicit Intent:

Don’t name a target and the field for the component name is left blank. Implicit intents are often used to activate components in other applications. For example:

Intent read = new Intent();
read.setAction(android.content.Intent.ACTION_VIEW);
read.setData(Uri.parse("http://www.google.com"));
startActivity(read);

// or call somebody

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:555–555–5555"));
activity.startActivity(intent);

Today we are going to learn about Explicit Intent.

How to start one Activity from another Activity?

We can start Activity B from Activity A using  startActivity(intent). In the below example, I want to start DisplayActivity from MainActivity when the user press the submit button.

Kotlin Code:

val intent = Intent(context, DisplayActivity::class.java)
startActivity(intent)

Java Code:

Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
startActivity(intent);

How to Pass data from one Activity to another?

I want to pass a movie name and movie description from MainActivity to DisplayActivity. We can pass data with intent using putExtra() method or with Bundle. For example,

DisplayActivity.kt (Kotlin code)

class DisplayActivity : AppCompatActivity() {

    companion object {
        fun getIntent(context: Context, movieName: String, description: String): Intent {
            val intent = Intent(context, DisplayActivity::class.java)
            intent.putExtra(MOVIE_NAME, movieName)
            intent.putExtra(DESCRIPTION, description)
            return intent
        }
    }
}

 

MainActivity.kt (Kotlin code)

class MainActivity : AppCompatActivity(), View.OnClickListener {

      override fun onClick(clickView: View?) {
        when (clickView?.id) {
            R.id.btnSubmit -> {
                sendSingleData()
            }
        }
    }

       private fun sendSingleData() {
        startActivity(DisplayActivity.getIntent(this@MainActivity, editMovieName.text.toString().trim()
                , editDescription.text.toString().trim()))
    }
}

 

UserDefault.kt (Kotlin code)

class UserDefault {
    companion object {
        const val MOVIE_NAME = "movie_name"
        const val DESCRIPTION = "description"
        const val MOVIE_DATA = "movie_data"

        //Result data
        const val MOVIE_RESULT = 1001
    }
}

 

DisplayActivity.java (Java code)

public class DisplayActivity extends AppCompatActivity {

  static Intent getIntent(Context context, String movieName, String description) {
        Intent intent = new Intent(context, DisplayActivity.class);
        intent.putExtra(UserDefault.MOVIE_NAME, movieName);
        intent.putExtra(UserDefault.DESCRIPTION, description);
        return intent;
    }

}

 

MainActivity.java (Java code)

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

  @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnSubmit: {
                sendSingleData();
                break;
            }
        }
    }

private void sendSingleData() {
        startActivity(DisplayActivity.getIntent(this, editMovieName.getText().toString()
                , editDescription.getText().toString()));
    }

}

 

UserDefault.java (Java code)

public class UserDefault {
    public static final String MOVIE_NAME = "movie_name";
    public static final String DESCRIPTION = "description";
    public static final String MOVIE_DATA = "movie_data";

    //Result Code
    public static final int MOVIE_RESULT = 1001;
}

 

Here as you can see I have added movie name and movie description using putExtra(string) method.

Different forms of putExtra() method.

  • putExtra (String name, Parcelable value)
  • putExtra (String name, long[] value)
  • putExtra (String name, byte value)
  • putExtra (String name, double[] value)
  • putExtra (String name, CharSequence value)
  • putExtra (String name, boolean[] value)
  • putExtra (String name, int value)
  • putExtra (String name, char[] value)
  • putExtra (String name, byte[] value)
  • putExtra (String name, Parcelable[] value)
  • putExtra (String name, Bundle value)
  • putExtra (String name, CharSequence[] value)
  • putExtra (String name, float[] value)
  • putExtra (String name, double value)
  • putExtra (String name, int[] value)
  • putExtra (String name, String[] value)
  • putExtra (String name, short[] value)
  • putExtra (String name, boolean value)
  • putExtra (String name, String value)
  • putExtra (String name, long value)
  • putExtra (String name, char value)
  • putExtra (String name, Serializable value)
  • putExtra (String name, float value)

We can retrieve data using getIntent().getStringExtra("Key") 

DisplayActivity.kt (Kotlin code)

class DisplayActivity : AppCompatActivity() {

   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_display)
        intent.extras?.let { bundle ->
                        textMovieName.text = bundle.getString(MOVIE_NAME)
            textDescription.text = bundle.getString(DESCRIPTION)

            val movieModel: MovieModel = bundle.getSerializable(MOVIE_DATA) as MovieModel
            movieModel?.let { movie ->
                textMovieName.text = movieModel.movieName
                textDescription.text = movieModel.description
            }
        }
    }
}
}

 

DisplayActivity.java (Java code)

public class DisplayActivity extends AppCompatActivity {

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);

        initView();
    }

    private void initView() {
        textMovieName = findViewById(R.id.textMovieName);
        textDescription = findViewById(R.id.textDescription);

        if (getIntent().getExtras() != null) {
            textMovieName.setText(getIntent().getStringExtra(UserDefault.MOVIE_NAME));
            textDescription.setText(getIntent().getStringExtra(UserDefault.DESCRIPTION));

            MovieModel movieModel = (MovieModel) getIntent().getSerializableExtra(UserDefault.MOVIE_DATA);
            if (movieModel != null) {
                textMovieName.setText(movieModel.getMovieName());
                textDescription.setText(movieModel.getDescription());

            }
        }
    }

}

It is always good practice to check if intent contains any data or not you can check this using if(getIntent().getExtras() != null) in Java and for Kotlin you can check using intent.extras?.let { }. You can retrieve data from Intent using the following methods.

  • public boolean getBooleanExtra (String name, boolean defaultValue)
  • public String getStringExtra (String name)
  • public Serializable getSerializableExtra (String name)
  • public char getCharExtra (String name, char defaultValue)
  • public byte getByteExtra (String name, byte defaultValue)
  • public double getDoubleExtra (String name, double defaultValue)
  • public float getFloatExtra (String name, float defaultValue)

Those are some common methods you can check all other methods on Android official site.

Source Code

Github project for Intent Demo

 

Related posts

Leave a Reply

Required fields are marked *