I have a TabHost
containing 7 tabs and have 10 items(listView) in each tab. It is highly impossible to create 70 Intent
s to handle request from each item.So can anyone provide the best way to handle them like one intent for each tab (i.e. for 10 items) but with different data. Just the data in TextView
changes.

- 1,190
- 13
- 18

- 23
- 1
- 5
2 Answers
You can pass data using intent will launching it.
Add data like this
Intent intent = new Intent(current.this, AndroidTabRestaurantDescSearchListView.class);
intent.putExtra("keyName","value");
startActivity(intent);
Retrieve like this:
String data = getIntent().getExtras().getString("keyName");

- 1
- 1

- 8,099
- 2
- 25
- 41
Have you considered using something like EventBus? (https://github.com/greenrobot/EventBus)
For example, you want to pass some data from one fragment to another:
First, create a data class, e.g. I want to pass string between fragments:
public class EventData {
/**
* Data
**/
private String mMessage;
/**
* Constructor
**/
public EventData(String message) {
mMessage = message;
}
/**
* GET / SET
**/
public void setMessage(String message) {
mMessage = message;
}
public String getMessage() {
return mMessage;
}
}
FragmentA is a receiver, in this class you receive data passed by event bus:
public class FragmentA extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container, false);
// Register this class to handle events from event bus
EventBus.getDefault().register(this);
return view;
}
/**
* This method would be fired upon event arrives
* @param data data, passed with an event. (In fact Argument can be any type)
**/
@Subscribe
public void onMessageEvent(final EventData data) {
// do some code
}
}
FragmentB: this class is a sender of data
public class FragmentB extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_b, container, false);
// Now just post something to fragment A
EventBus.getDefault().post(
new EventData("I am a message!");
);
return view;
}
}
Please do note that this way your data sent to Event Bus is accessible through all app, meaning that you can have multiple fragments registered to receive this event. Generally, it is a good thing, although sometimes there is a need to extra check is this is the intended event (e.g. assign unique IDs to every event)

- 1,190
- 13
- 18