Following situation: I have TabActivity with e.g. three tabs, TabA, TabB, TabC.
There are a button in activity (Act_C_1) of TabC. So if the user clicks on that button, another activity (Act_C_2) should occur in TabC.
I thank you in advance for any suggestions / or ideas.
Mur
UPD:
Here is my code
TabActivity with three Activities:
public class TabScreen extends TabActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_menu);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, SecondActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tab_1").setIndicator("Tab1",null).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ThirdActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tab_2").setIndicator("Tab2",null).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, FourthActivity.class);
spec = tabHost.newTabSpec("tab_3").setIndicator("Tab3",null).setContent(intent);
tabHost.addTab(spec);
}
}
Activity 'Act_C_1' or FourthActivity.java:
public class FourthActivity extends Activity implements OnClickListener
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fourth);
Button BtnWeiter = (Button)findViewById(R.id.BtnWeiter);
BtnWeiter.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
// I also tried to use LocalActivityManager
// TabActivity parentTabActivity = (TabActivity) getParent();
// LocalActivityManager manager = parentTabActivity.getLocalActivityManager();
// manager.destroyActivity("tab_3", true);
// manager.startActivity("tab_3", new Intent(this, FourthActivity.class));
finish();
startActivity(new Intent(this, FourthActivity.class));
}
}
Just use Intent.FLAG_ACTIVITY_CLEAR_TOP flag to replace an activity for necessary tab.
The Activities in the tab can be switched in the following manner.
First Let us understand the flow:
We have in a Tab host , activity (say a list) from which we need to go to the next Activity (say details for the clicked item) under the same tab. For this we can use the concept of replacing the activity.Also setting the flags for the tab selected and other for knowing that details are being shown now
When we press back we should get the previous activity under the same tab.For this instead of again replacing the activity we can refresh the tab while using the particular flag for tab which was selected. Also if flag for show details is true we'll go the the list in the same tab or else we will go the activity before the tabwidget (normal use of onBackPressed)
The code can be as follows..
(This can be in the onClickListener)
When back pressed is done we override on BackPressed in each of the Activity under the tab to go to the list again from the details screen
The most important part here is Constants.LIST_ACTIVITY = 1; it indicates which tab we are in. so the corresponding activities will have its value as 0,1,2...etc
Again to load the correct list (Activty) when the tab activity is refreshed we have to include this in the TabWidget onCreate after the creation of the tabs
Just add this for all tabs:
Handle the onClick event for button
finish() the activity Add startActivity method to start another activity.
Thanks