Monday, January 28, 2019


It is very useful to have an app that is able to access different Activities and pass values (Intent) that can be used by the new Activity.






In file /MainActivity.java import the intent class.

import android.content.Intent;



Now paste the following code inside MainActivity class

Intent i = new Intent(MainActivity.this.getApplicationContext(), NewActivity.class);


NewActivity: This is the name of the class of the activity you wish to load on your app.

Now use PushExtra to pass values into the NewActivity.

i.putExtra("title", "Any Text");
i.putExtra("image", R.drawable.harzard_sign);


looking above, line_1 shows how to pass a String valuable, and line_2 show how to pass
a drawable.

Once you done, you may start new activity.

startActivity(i);


Now lets access your /New_Activity.java file, first import the intent class

import android.content.Intent;


Now use the below code to access the values you just pass.

//get valuables
Intent intent = getIntent();
String menu_title = intent.getExtras().getString("title_text");
int menu_icon = intent.getExtras().getInt("image_view");


I hope this helps you will your android development journey.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Mdunge Apps - Blogger Templates - Powered by Blogger - Designed by Johanes Djogan -