Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple example for Intent and Bundle [duplicate]

I'm new to android with almost no knowledge about Java and XML. I'm learning it through pdfs that i'm getting on net. I have learnt about Toast, a bit about Intents but me not able to understand anything about Bundles. I have understood that they are used to pass data from one activity to another but I'm not able to implement this.

please give a simple example to implement the same.

as for example I have just created two activities namely , Main_Activity and Other_Activity, and i haven't done anything to them yet.

Please give the simplest example so that i can learn to implement.

Thanks in advance!!

like image 778
Akshay Sethi Avatar asked Feb 14 '13 13:02

Akshay Sethi


People also ask

What is Intent explain with example?

An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents. Here is a sample example to start new activity with old activity.

What is the difference between a bundle and an Intent?

Bundles are used with intent and values are sent and retrieved in the same fashion, as it is done in the case of Intent. It depends on the user what type of values the user wants to pass, but bundles can hold all types of values (int, String, boolean, char) and pass them to the new activity.

Whats the difference between putExtra and PutExtras methods used in Intent?

PutExtra() - we can store any primitive data type directly with (key,value) pair. And PutExtras() - hold object of Bundle class object . Bundle class provide us method of specific primitive data type methods to store data in it.


2 Answers

For example :

In MainActivity :

Intent intent = new Intent(this, OtherActivity.class); intent.putExtra(OtherActivity.KEY_EXTRA, yourDataObject); startActivity(intent); 

In OtherActivity :

public static final String KEY_EXTRA = "com.example.yourapp.KEY_BOOK";  @Override protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);    String yourDataObject = null;    if (getIntent().hasExtra(KEY_EXTRA)) {       yourDataObject = getIntent().getStringExtra(KEY_EXTRA);   } else {       throw new IllegalArgumentException("Activity cannot find  extras " + KEY_EXTRA);   }   // do stuff } 

More informations here : http://developer.android.com/reference/android/content/Intent.html

like image 92
Dany Pop Avatar answered Sep 19 '22 12:09

Dany Pop


Try this: if you need pass values between the activities you use this...

This is code for Main_Activity put the values to intent

 String name="aaaa";  Intent intent=new Intent(Main_Activity.this,Other_Activity.class);  intent.putExtra("name", name);  startActivity(intent); 

This code for Other_Activity and get the values form intent

    Bundle b = new Bundle();     b = getIntent().getExtras();     String name = b.getString("name"); 
like image 28
Android_coder Avatar answered Sep 19 '22 12:09

Android_coder