Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using intents to pass data between activities

I am trying to pass the data between Activities

I use intents to pass data between regular activities

consider the code below::

AndroidTabRestaurantDescSearchListView.java

public class AndroidTabRestaurantDescSearchListView extends TabActivity {      // TabSpec Names     private static final String INBOX_SPEC = "Rating";     private static final String OUTBOX_SPEC = "Price";      Button Photos;     Button Filter;     Button Search;      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);          TabHost tabHost = getTabHost();          // Inbox Tab         TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);         Intent inboxIntent = new Intent(this, RatingDescriptionSearchActivity.class);         inboxSpec.setIndicator(INBOX_SPEC);         // Tab Content         inboxSpec.setContent(inboxIntent);          // Outbox Tab         TabSpec PriceSpec = tabHost.newTabSpec(OUTBOX_SPEC);         Intent PriceIntent = new Intent(this, PriceDescriptionSearchActivity.class);         PriceSpec .setIndicator(OUTBOX_SPEC);         PriceSpec.setContent(PriceIntent);           // Adding all TabSpec to TabHost         tabHost.addTab(inboxSpec);          tabHost.addTab(PriceSpec);           //Set the current value tab to default first tab         tabHost.setCurrentTab(0);      }  } 

Suppose i send data from Someother activity called Activity-1 to AndroidTabRestaurantDescSearchListView as intents

Now how can i recieve the data in AndroidTabRestaurantDescSearchListView which i got from Activity-1 and then again pass it into RatingDescriptionSearchActivity

Pictoral representation is ::

enter image description here

{EDIT} -- If this is possible based on answers --- Ambiguity because AndroidTabRestaurantDescSearchListView is a tab activity

TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);         Intent inboxIntent = new Intent(this, RatingDescriptionActivity.class);         intent.putExtra("keyName", value);         inboxSpec.setIndicator(INBOX_SPEC);         // Tab Content         inboxSpec.setContent(inboxIntent); 
like image 369
Devrath Avatar asked Oct 10 '13 04:10

Devrath


People also ask

How do you pass data between two activities through Intent?

To pass data between two activities, you will need to use the Intent class via which you are starting the Activity and just before startActivity for ActivityB, you can populate it with data via the Extra objects. In your case, it will be the content of the editText.

What way is used to pass data between activities?

We can send the data using putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

What is passed between activities when launching an Intent?

Starting an activity You can start a new instance of an Activity by passing an Intent to startActivity() . The Intent describes the activity to start and carries any necessary data. If you want to receive a result from the activity when it finishes, call startActivityForResult() .

How do you send data to multiple activities?

Intent i = new Intent(this, ActivityB. class); i. putExtra("DATA1", "Hello"); i. putExtra("DATA2", "World"); startActivity(i);


2 Answers

Pass the data from Activity-1 to AndroidTabRes.. as below:

At sending activity...

Intent intent = new Intent(current.this, AndroidTabRestaurantDescSearchListView.class); intent.putExtra("keyName","value"); startActivity(intent); 

At AndroidTabRes.. activity...

  String data = getIntent().getExtras().getString("keyName"); 

Thus you can have data at receiving activity from sending activity...

And in your AndroidTabRestaurantDescSearchListView class, do this:

String value= getIntent().getStringExtra("keyName");  Intent intent = new Intent(this, RatingDescriptionSearchActivity.class); intent.putExtra("keyName", value); startActivity(intent); 

Then in your RatingDescriptionSearchActivity class, do this:

 String data= getIntent().getStringExtra("keyName"); 
like image 169
GrIsHu Avatar answered Sep 18 '22 07:09

GrIsHu


Try this from your AndroidTabRestaurantDescSearchListView activity

Intent intent  = new Intent(this,RatingDescriptionSearchActivity.class ); intent.putExtras( getIntent().getExtras() ); startActivity( intent );   

And then from RatingDescriptionSearchActivity activity just call

getIntent().getStringExtra("key") 
like image 30
Jans Avatar answered Sep 20 '22 07:09

Jans