Here is the scenario: I have an activity with 4 tabs, each tab with a different intent, each intent with a different activity. Works perfectly.
What I need is, to somehow change the intent on one of the tabs.
It would be as simple as adding extra parameters to the Intent that is used on the TabSpec.setContent(intent)
, but I have not found a way to retrieve that intent to change it.
The specific action I'm trying to do is: from another activity (that i开发者_开发知识库s inside the content of another tab) I call it's parent (the TabActivity
) to open a different tab AND add some custom data to that tab's activity.
I can change the tab without a problem, but I haven't found a way to pass the extra parameters from one activity to the other, since the original intent used to create the TabSpec
had no extra parameters.
Am I approaching this in the wrong way?
Is there a way to replace the TabSpec
content's intent?
Thanks !
If you are just trying to find a way to pass values between activities you can override the Application class.
Way to use app context.
Extend the application class and add your values as its attributes. In any activity, if you call the below code, it will return a singleton.
MyApplication appContext = (MyApplication) getApplicationContext();
To make this work you need to add this to the application tag of the manifest file
android:name=".MyApplication"
This method is used to pass values around the app when sending through intent is not possible.
To remove the tab, you will need to use the clearAllTabs() method and add the tabs again. The above code should be better.
In my case I'm storing a reference to an intent in the TabActivity
mGalleryTabIntent = new Intent(this, AnActivity.class);
spec = getTabHost().newTabSpec(TAB_GALLERY).setIndicator(res.getString(R.string.footer_gallery),res.getDrawable(R.drawable.gallery_icon_sel)).setContent(mGalleryTabIntent);
public Intent getStoredTabIntent(){
return mGalleryTabIntent;
}
Then, when from a child I want to navigate to another tab passing an Extra along with the Intent
MainTabActivity parent = (MainTabActivity)getParent();
parent.getStoredTabIntent().putExtra(AnActivity.START_VIEW, AnActivity.PAGE_TWO);
//Navigate to the tab
parent.getTabHost().setCurrentTabByTag(AnActivity.TAB_GALLERY);
Then, in AnActivity's onCreate
Bundle extras = getIntent().getExtras();
if(extras != null && extras.containsKey(START_VIEW)){
switch (extras.getInt(START_VIEW)) {
case PAGE_TWO:
doSomething();
break;
default:
break;
}
//Erase the Extra so that navigating to this Tab always displays the standard view unless specified otherwise
MainTabActivity parent = (MainTabActivity)getParent();
parent.getStoredTabIntent().putExtra(AnActivity.START_VIEW, "");
}else{
doStandardStuff();
}
The Application solution is also good, but I don't think I'll be needing it for anything else, thus I'd rather stick with Activities.
TabSpec tabspec1;
private void createTabHost() {
// initialize tabHost
tabHost = (TabHost) this.findViewById(R.id.tabhost1);
tabHost.setup(this.getLocalActivityManager());
// create tab1
Intent intent = new Intent(this, AnotherActivity.class).addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("myData",
ObjData);
tabspec1 = tabHost.newTabSpec("tab1")
.setIndicator(Utilities
.prepareTabView(this, "Title"))
.setContent(intent);
tabHost.addTab(tabspec1 );
// create tab2
...
}
method that changes the content of that activity placed in your tab:
private void reloadTabSpec1() {
Intent i = new Intent(this, AnotherActivity.class).addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("myData", ObjData);
tabspec1.setContent(i);
// needed for refresh :(
tabHost.setCurrentTabByTag(tabspec2.getTag());
tabHost.setCurrentTabByTag(tabspec1.getTag());
}
精彩评论