I have three activities:
Activity A – Displays a list of data from a website. From this activity you can perform an action which would add something to this list (Activity B).
Activity B – This is the activity that adds data to the website. Upon successful completion this activity will need to tell Activity A to refresh its list.
Activity T – A TabHost which contains Activity A.
My problem is:
Activity B needs to tell Activity A to refresh, but its intent must be Activity T, since I want the TabHost to display Activity A.
//ActivityB.class
Intent myIntent = new Intent(v.getContext(), ActivityT.class);
myIntent.putExtra("target", "ActivityA");
myIntent.putExtra("refreshData", true);
startActivityForResult(myIntent, 0);
I thought I could use the TabHost as an intermediary and just pass the extras through to Activity A if they were set, like so:
//ActivityT.class
Bundle extras = getIntent().getExtras();
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ActivityA.class);
// Check to see if we want to pass our bundle through to the activity
if (extras != null && extras.getString("target").equals("activityA")) {
intent.putExtras(extras);
}
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Tab1").setIndicator("Tab1",
res.getDrawable(R.drawable.ic_tab_tab1)).setContent(intent);
tabHost.addTab(spec);
This works great, except that when I change tabs to a different tab and change back, the extras are passed through yet again, and again – which causes my tab to refresh its data every time, even though I’m no longer coming from Activity B. This is because the TabActivity doesn’t get recreated on tab change and therefore the extras are always passed through to the child intents.
So, how I can tell Activity A to refresh ONLY from Activity B, but still get the TabHost to show up?
While perhaps not the most elegant solution, I used static data members in my ActivityA class which I can set from ActivityB.
I’m interested to know if there’s a better way to do this kind of thing!