not run oncreate() ,on tab change in same page, 2nd time - android

I have 4 tabs in a page view when i click first time on tab it executes it oncreate() method of corresponding tab and when i go to other tab on same page and again click on previous clicked tab then its oncreate() method not execute why?why it is not work as button click means each time i click its oncreate() method run.
my code for tab activity class is below
intent1 = new Intent().setClass(this, keywordxmlparsing.class);
spec1 = tabHost.newTabSpec("Activity2").setIndicator("keyword/ search...").setContent(intent1);
tabHost.addTab(spec1);
intent2 = new Intent().setClass(this, filter.class);
spec2 = tabHost.newTabSpec("Activity1").setIndicator("filter search").setContent(intent2);
tabHost.addTab(spec2);
intent3 = new Intent().setClass(this, OpeningToday.class);
spec3 = tabHost.newTabSpec("Activity3").setIndicator("opening today").setContent(intent3);
tabHost.addTab(spec3);
intent4 = new Intent(keywordresulttab.this,Map.class);
spec4 = tabHost.newTabSpec("Activity4").setIndicator("Map").setContent(intent4);
tabHost.addTab(spec4);
dear if i use
#Override
public void onTabChanged(String label) {
// TODO Auto-generated method stub
if(label == "Activity2") {
}
try{
if(label == "Activity4") {
Intent intent4;
intent4 = new Intent(keywordresulttab.this,Map.class);
startActivity(intent4);
Log.i("suiuawhd","maps class");
}
then can i go to other activity means using
intent4 = new Intent(keywordresulttab.this,Map.class);
startActivity(intent4);
we can load again activity on tab click??then what to written in place of
tabHost.addTab(spec3);
intent4 = new Intent(keywordresulttab.this,Map.class);
spec4 = tabHost.newTabSpec("Activity4").setIndicator("Map").setContent(intent4);
tabHost.addTab(spec4);

Tabs that contain activities are implemented by means of ActivityGroup. When someone changes a tab, corresponding activity is created only if necessary. When you move to any tab for the second time, the activity is already existing and only its window is shown.
You should use instead:
TabHost.setOnTabChangedListener(TabHost.OnTabChangeListener l)
TabActivity with separate activities as a content are a little bit tricky so maybe you should consider using Views instead. If not you can use the following code to access all activities from each other:
get instances of content activities from TabActivity:
TabActivity.getLocalActivityManager().getActivity(String name)
where name is given in newTabSpec() method.
get instance of TabActivity from content activities:
FirstTab.getParent()
Using these method you can create communication between all activities (using proper casting). For example:
public void onTabChanged(String label) {
if(label.equals("Activity2")) {
SecondActivity sa = (SecondActivity) getLocalActivityManager().getActivity(label);
sa.modifySomething();
}
}
If you want to change activities under tabs you should use:
tabHost.clearAllTabs ()
and create all TabSpecs once again.

In the android oncreate() lifecycle called once in life of the activity (like constructor), as you press again that time the activity is alive and in memory so it will not call onCreate again.
so you have to implement
TabHost.setOnTabChangedListener(TabHost.OnTabChangeListener l).

Related

making more than one activity in a tab

I really want to move from an activity within a tab view to another activity within the same tab.
public void onClick (View view) {
// This creates an intent to call the 'Called' activity
i1 = new Intent(this.getBaseContext(),Called.class);
// calls the method to replace View.
replaceContentView("Called", i1);
}
try this...
TabSpec spec=...//our tabSpec
Intent intent = new Intent(this.getBaseContext(), Called.class);
spec.setContent(videosIntent);

How to return to a Parent Activity from a sub activity when using Tabs in Android

I have two tabs A, B with corresponding TabActivityA and TabActivityB. I have a third ActivityA1 which is not on the tab but is an intermediate activity coming from ActivityA.
Here is the code in sequence
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for Home
TabSpec homeSpec = tabHost.newTabSpec("Home");
homeSpec.setIndicator("Home", getResources().getDrawable(R.drawable.icon_home_tab));
Intent homeIntent = new Intent(this, TabActivityA.class);
homeSpec.setContent(homeIntent);
// Tab for my cases
TabSpec helppec = tabHost.newTabSpec("Help");
// setting Title and Icon for the Tab
helppec.setIndicator("Help", getResources().getDrawable(R.drawable.icon_cases_tab));
Intent helpIntent = new Intent(this, TabActivityB.class);
mycasesspec.setContent(helpIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(homeSpec); // Adding home tab
tabHost.addTab(help); // Adding help tab
}
ActivityA1 extends Activity
{
}
TabActivityA extends ActivityGroup
{
.....
Intent nextScreen = new Intent(getApplicationContext(), ActivityA1.class);
View view = getLocalActivityManager().startActivity("ActivityA1", nextScreen.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)).getDecorView();
setContentView(view);
}
Note: I am doing this because I want to show the same tabs for ActivityA1
This does show the tab (Home and Help) on ActivityA1, however on clicking the Home tab I want the user to go to TabActivityA but right now it is staying on ActivityA1 only.
Any help will be greatly appreciated!!
Ok I think I understand what you are asking now, simply keep an arrayList of View, add to this list before you call SetContentView, then when you are ready to go back to the previous View do SetContentView(list[count - 1]), also make sure you remove the previous View and dispose of it or it will hang around in memory forever...

Activity in TabHost

I use a TabHost.
The below code to call AActivity.
intent = new Intent().setClass(this, AActivity.class);
spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
And it is in the tab.
But in AActivity I call BActivity.
The BActivity will open new page, but not in the tab.
How to let it on the tab frame?
AActivity use below code to call BActivity:
it = new Intent(this, BActivity.class);
startActivity(it);
If you want to open multiple activity in Tab then on Place of activity use Activity group for par tab and switch view in this activity group for open multiple Activity in single tab
you can take some help from this tutorial
You need to change the current selected tab. There's a method called setCurrentTabByTag(String tag) in the TabHost class that will do that, just pass the tag name of your tab (which in your case is B), or you can use the setCurrentTab(int index) and pass the tab index.
Example. Usually I have a MainActivity class, which is my TabActivity. Inside of this class, I will create all tabs that I need on the onCreate method.
First I set some static int with the tabs indexes.
// Tab index.
public static int FIRST_TAB = 0;
public static int SECOND_TAB = 1;
Later, I create my tabs in the onCreate method.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Setting the content view.
setContentView(R.layout.main);
// Getting the TabHost object.
mTabHost = getTabHost();
// Declaring the Intent object for the tabs.
Intent intent;
// Creating the First tab.
intent = new Intent().setClass(this, MyFirstActivity.class);
addTab(mTabHost, "First", FIRST_TAB, intent)
// Creating the Second tab.
intent = new Intent().setClass(this, MySecondActivity.class);
addTab(mTabHost, "Second", SECOND_TAB, intent);
// Setting the current tab.
switchTab(FIRST_TAB);
}
public void addTab(TabHost host, String title, String tag, Intent intent) {
TabHost.TabSpec spec = host.newTabSpec(tag);
spec.setContent(intent);
spec.setIndicator(title);
host.addTab(spec);
}
And last the method that will change the current tab.
public void switchTab(int index) {
mTabHost.setCurrentTab(index);
}
Later, inside of the MyFirstActivity you can call the MainActivity swichTab method and pass the index of the tab to change it.
You can retrieve the MainActivity calling the getParent() method of the Activity class.
MainActivity parent = (MainActivity)getParent();
In the tab activity class where the tabhost is created, implement the following method.
public void switchTab(int tab){
tabHost.setCurrentTab(tab);
}
In AActivity/BActivity implement the following method and call it on any event(that you need):
public void switchTabInActivity(long indexTabToSwitchTo){
TabActivity tabActivity;
tabActivity = (TabActivity) this.getParent();
tabActivity.switchTab(indexTabToSwitchTo);
}
Here TabActivity is the class where tabhost is created

How to change the intent on a TabSpec

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 is 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());
}

Switch between Android tabs using intents

I wish to find out how to switch between tabs using intents.
In my case I'm using the both tabs:
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
// Capture tab
spec = tabHost.newTabSpec("capture").setIndicator(null,
res.getDrawable(R.drawable.ic_tab_capture))
.setContent(new Intent(this,CaptureActivity.class));
tabHost.addTab(spec);
// Upload tab
spec = tabHost.newTabSpec("upload").setIndicator(null,
res.getDrawable(R.drawable.ic_tab_capture))
.setContent(new Intent(this,ImageUpload.class));
tabHost.addTab(spec);
To simplify my goal, my CaptureActivity.java includes the following code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.capture);
Intent intent = new Intent(this, ImageUpload.class);
startActivityForResult(intent, 0);
}
What I'm expecting is, the app should switch instantly to the second tab (ImageUpload activity) which works fine, BUT the tabs themselves disappear. I get the ImageUpload activity as stand-alone page, not within the tab itself.
Any idea what's going wrong there ?
First calling firing ImageUpload.java only fires ImageUpload.class but surely tabhost will disappear.
You need to fire your MainActivity-TabActivity where you added your two tabHost
1.ImageUpload
2.CaptureActivity
which will maintain your TabLayout
call intent like these
Intent i=new Intent(getApplicationContext(),MainActivity.class)//which is your mainActivity-Launcher
i.addFlags(Intent.FLAG_ACTIVITY_BRING_TO_FRONT);
startActivity(i);//will bring MainACtivity to Front
Now Main activity is already running so pointer directly goes to onNewIntent() inside Main Activity
Override onNewIntent() inside MainActivity
=====================================
public class MainActivity extends TabActivty
{
pubilc static TabHost tabhost;
public void onCreate()
{
//where you added your two tab spec
//added them
}
public void onNewIntent(intent)
{
super.onNewIntent(intent);
tabHost.setCurrentTab(1);//1-depends where you want to switch-tabIndexno, I assume Upload is at 2nd position (so "1")
Activity currentActivity=getCurrentActivity();
if(currentActivity instanceof Upload)
{
((upload)currentActivity).onCreate();//watever method you want to call
}
}
}
In the parent activity class where the tabhost is created I implemented a method like the one below:
public void switchTab(int tab){
tabHost.setCurrentTab(tab);
}
Inside of the tab that I would like to be able to switch internally to another tab I created the method below:
public void switchTabInActivity(int indexTabToSwitchTo){
YOURTABHOSTACTIVITY ParentActivity;
ParentActivity = (YOURTABHOSTACTIVITY) this.getParent();
ParentActivity.switchTab(indexTabToSwitchTo);
}
If you would like a good example of this code, you can take a look at my MintTrack project here and here.

Categories

Resources