I have a tabhost and I add there 3 Activities(one activity per tab).
I need to know how to call a new intern in an activity each time i change a tab.
I added a listener for the tabhost.When I use the clearAllTabs(); method and add all the tabs again inside the listener then the app crash.
when I use code toto delete from the view the specific tab that the user clickes tabHost.getTabWidget().removeView(tabHost.getTabWidget().getChildTabViewAt(i));
tabHost.addTab(the tab I want to replace);
then the new tab is positioned in the end of the tabhost.
I just need an example of how to reload the proportionate activity each time the user clickes the specific tab.
my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ActionBar bar = getSupportActionBar();
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Resources res = getResources();
LocalActivityManager mlam = new LocalActivityManager(this, false);
final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
mlam.dispatchCreate(savedInstanceState);
tabHost.setup(mlam);
TabHost.TabSpec spec;
Intent intent;
// TabHost tabHost = getTabHost();
// tabHost.setup();
TabSpec specAll = tabHost.newTabSpec("All");
specAll.setIndicator("All");
Intent allIntent = new Intent(this, allActivity.class);
specAll.setContent(allIntent);
// specAll.setContent(R.id.allList);
Log.d("SpecAll",""+specAll.setContent(allIntent));
TabSpec specIn = tabHost.newTabSpec("in");
specIn.setIndicator("In");
Intent inIntent = new Intent(this, inActivity.class);
specIn.setContent(inIntent);
TabSpec specOut = tabHost.newTabSpec("Out");
specOut.setIndicator("Out");
Intent outIntent = new Intent(this, outActivity.class);
specOut.setContent(outIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(specAll); // Adding all tab
tabHost.addTab(specIn); // Adding in tab
tabHost.addTab(specOut); // Adding out tab
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
int i = tabHost.getCurrentTab();
//Log.i("######## ANN CLICK TAB NUMBER", "------" + i);
if (i == 0) {
Log.d("TAB","" +i);
} else if (i == 1) {
Log.d("TAB","" +i);
}
else
Log.d("TAB", ""+i);
}
});
}
It seems there is a matter with activity and tabhost.in order to reload an activity you just have to do:
specAll.setContent(yourIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
Just before the tabHost.addTab
In my simple case when I do not need to persist tab fragments I use the next code
int currentTabId = mTabHost.getCurrentTab();
mTabHost.clearAllTabs();
setupTabs();
mTabHost.setCurrentTab(currentTabId);
I also face the same problem but i resolve this issue like this way...
This is my TabActivity....
public class MainActivity extends TabActivity {
TabHost tabhost;
String cTab = "0";
String nTab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
tabhost = getTabHost();
TabSpec one = tabhost.newTabSpec("0");
// setting Title and Icon for the Tab
one.setIndicator("", getResources().getDrawable(R.drawable.ic_launcher));
Intent songsIntent = new Intent(this, FirstActivity.class);
one.setContent(songsIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
one.setContent(songsIntent);
TabSpec two = tabhost.newTabSpec("1");
// setting Title and Icon for the Tab
two.setIndicator("", getResources().getDrawable(R.drawable.ic_launcher));
Intent songsIntent1 = new Intent(this, SecondActivity.class);
two.setContent(songsIntent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
two.setContent(songsIntent1);
TabSpec three = tabhost.newTabSpec("2");
// setting Title and Icon for the Tab
three.setIndicator("",
getResources().getDrawable(R.drawable.ic_launcher));
Intent songsIntent4 = new Intent(this, ThirdActivity.class);
three.setContent(songsIntent4.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
three.setContent(songsIntent4);
TabSpec four = tabhost.newTabSpec("3");
// setting Title and Icon for the Tab
four.setIndicator("", getResources()
.getDrawable(R.drawable.ic_launcher));
Intent songsIntent5 = new Intent(this, FourthActivity.class);
four.setContent(songsIntent5.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
four.setContent(songsIntent5);
tabhost.addTab(one);
tabhost.addTab(two);
tabhost.addTab(three);
tabhost.addTab(four);
tabhost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
cTab = "" + tabhost.getCurrentTab();
}
});
int numberOfTabs = tabhost.getTabWidget().getChildCount();
for (int t = 0; t < numberOfTabs; t++) {
tabhost.getTabWidget().getChildAt(t)
.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
String currentSelectedTag = MainActivity.this
.getTabHost().getCurrentTabTag();
nTab = currentSelectedTag;
System.out.println(" nTab " + nTab);
System.out.println(" cTab " + cTab);
if (cTab.equals(nTab)) {
if (nTab.equals("0")) {
Intent intent = new Intent();
intent.setAction("com.ensis.first");
MainActivity.this.sendBroadcast(intent);
}
if (nTab.equals("1")) {
Intent intent = new Intent();
intent.setAction("com.ensis.second");
MainActivity.this.sendBroadcast(intent);
}
if (nTab.equals("2")) {
Intent intent = new Intent();
intent.setAction("com.ensis.third");
MainActivity.this.sendBroadcast(intent);
}
if (nTab.equals("3")) {
Intent intent = new Intent();
intent.setAction("com.ensis.fourth");
MainActivity.this.sendBroadcast(intent);
}
}
}
return false;
}
});
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}
This is my FirstActivity.java
public class FirstActivity extends ActivityGroup{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ok();
IntentFilter filter = new IntentFilter("com.ensis.first");
registerReceiver(myReceiver, filter);
/**/
}
private void ok() {
// TODO Auto-generated method stub
setContentView(R.layout.firstscreen);
Button bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it=new Intent(FirstActivity.this,SubActivity.class);
replaceContentView("activity3", it);
}
});
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
this.setContentView(view);
}
private BroadcastReceiver myReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent) {
Log.v("22222222222222222", "22222222222");
ok();
}
};}
Related
Is it possible to add a swipe functionality to an existing Tab Menu? i.e. swipe left or right to move between the tabs rather than clicking on a tab.
I already have an exisiting 3 tab menu and do not want to greatly alter it's structure, but would love to add the swipe functionality.
How can I do so?
Main Menu/TabHost:
public class MainMenu extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec tab1 = tabHost.newTabSpec("Games");
Intent tab1Intent = new Intent(this, firstActivity.class);
tab1.setContent(tab1Intent);
tab1.setIndicator("Games");
TabSpec tab2 = tabHost.newTabSpec("Search");
Intent tab2Intent = new Intent(this, secondActivity.class);
tab2.setContent(tab2Intent);
tab2.setIndicator("Search");
TabSpec tab3 = tabHost.newTabSpec("Summary");
Intent tab3Intent = new Intent(this, thirdActivity.class);
tab3.setContent(tab3Intent);
tab3.setIndicator("Summary");
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Example of tab (middle tab):
public class secondActivity extends ActionBarActivity implements View.OnClickListener {
TextView instruction;
Button date;
Button game;
Button med;
Button att;
Button score;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_tab);
initialiseVars();
}
/**
* Initialises vars and sets onclick listeners for buttons
*/
public void initialiseVars() {
// setting up vars
instruction = (TextView) findViewById(R.id.tvSearchHome);
date = (Button) findViewById(R.id.btnSearchHomeDate);
game = (Button) findViewById(R.id.btnSearchHomeGame);
med = (Button) findViewById(R.id.btnSearchHomeMedValues);
att = (Button) findViewById(R.id.btnSearchHomeAttValues);
score = (Button) findViewById(R.id.btnSearchHomeScore);
// set on click listeners for the buttons
date.setOnClickListener(this);
game.setOnClickListener(this);
med.setOnClickListener(this);
att.setOnClickListener(this);
score.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearchHomeDate:
Intent openDateSearch = new Intent(
"com.example.brianapp.SearchDate");
// Start activity
startActivity(openDateSearch);
break;
case R.id.btnSearchHomeGame:
Intent openGameSearch = new Intent(
"com.example.brianapp.SearchGame");
// Start activity
startActivity(openGameSearch);
break;
case R.id.btnSearchHomeMedValues:
// change this to make sure it opens the med screen
Intent openMedSearch = new Intent(
"com.example.brianapp.MeditationSearchHome");
// Start activity
startActivity(openMedSearch);
break;
case R.id.btnSearchHomeAttValues:
// change this to make sure it opens the med screen
Intent openAttSearch = new Intent(
"com.example.brianapp.AttentionSearchHome");
// Start activity
startActivity(openAttSearch);
break;
case R.id.btnSearchHomeScore:
// change this to make sure it opens the med screen
Intent openScoreSearch = new Intent(
"com.example.brianapp.SearchScore");
// Start activit
startActivity(openScoreSearch);
break;
}// switch end
}
}
I can just provide you with the following snippet. Please note that I haven't tested it. I just wanted to show how I would approach this problem.
public class MainMenu extends TabActivity implements GestureDetector.OnGestureListener{
...
#Override
public boolean onFling(MotionEvent e0, MotionEvent e1, float arg2, float arg3) {
...
int current = getCurrentTab(); // TabHost.getCurrentTab()
int next;
if(e0.getRawX() - e1.getRawX() < 0 ){
//fling right
next = current + 1; //TODO check for next = n, n=number of tabs
}
else{
next = current - 1; //TODO check for next = -1
}
setCurrentTab(next); // TabHost.setCurrentTab()
}
}
I have 2 tab while select first tab the accept menu icon should not visible.
while select second tab the accept menu icon should visible. I tried but i am getting null pointer exception.
And also should get the text from the second activity while select accept menu.
How can i do this ?
Here is my Code :
public class DetailTabActivity extends TabActivity implements OnTabChangeListener {
String name,address,rating,reference,lat,lng;
StringConstants constants;
Float rate;
MenuItem menuitem;
String url;
TabHost tabHost;
Menu menu1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.overridePendingTransition(R.anim.anim_out,
R.anim.anim_in);
constants=new StringConstants();
setContentView(R.layout.layout_detail_tab);
Intent intent = getIntent();
name = intent.getStringExtra("name");
Log.e("name",name+"");
address = intent.getStringExtra("address");
Log.e("address",address+"");
rating = intent.getStringExtra("rating");
Log.e("rating",rating+"");
reference = intent.getStringExtra("reference");
Log.e("reference",reference+"");
lat = intent.getStringExtra("lat");
Log.e("lat",lat+"");
lng = intent.getStringExtra("lng");
Log.e("lng",lng+"");
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
// Tab for Photos
TabSpec detailspec = tabHost.newTabSpec("Detail");
// setting Title and Icon for the Tab
detailspec.setIndicator("Detail");
Intent detailintent = new Intent(this, DetailActivity.class);
detailintent.putExtra("name",name);
detailintent.putExtra("address",address);
detailintent.putExtra("rating",rating);
detailintent.putExtra("reference",reference);
detailspec.setContent(detailintent);
// Tab for Songs
TabSpec reviewspec = tabHost.newTabSpec("Review");
reviewspec.setIndicator("Review/Rating");
Intent songsIntent = new Intent(this, ReviewAndRating.class);
songsIntent.putExtra("name",name);
songsIntent.putExtra("lat",lat);
songsIntent.putExtra("lng",lng);
reviewspec.setContent(songsIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(detailspec); // Adding photos tab
tabHost.addTab(reviewspec); // Adding songs tab
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#ffffff"));
// Set Tab1 as Default tab and change image
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#134960"));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.layout_detail_tab, menu);
menuitem = menu.findItem(R.id.action_accept);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_accept:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onTabChanged(String tabId) {
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
if(i==0)
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
else if(i==1)
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
}
Log.i("tabs", "CurrentTab: "+tabHost.getCurrentTab());
if(tabHost.getCurrentTab()==1)
{
menuitem.setVisible(true);
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
else if(tabHost.getCurrentTab()==0)
{
menuitem.setVisible(false);
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
}
}
set invalidateOptionsMenu();as,
if(tabHost.getCurrentTab()==1)
{
menuitem.setVisible(true);
invalidateOptionsMenu();
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
else if(tabHost.getCurrentTab()==0)
{
menuitem.setVisible(false);
invalidateOptionsMenu();
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
Here is the Code which I'm Trying. How to go to Tab2 from Sample? Please help me to Solve this.
public class DemoTab extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_and_login_tab);
setTabs() ;
}
private void setTabs()
{
addTab("Tab1", R.drawable.tab_search, Tab1.class);
addTab("Tab2", R.drawable.tab_home, Tab2.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
Code for Tab2
public class Tab2 extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
}
}
Code for Tab1
public class Tab1 extends Activity implements OnClickListener {
Button check;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
check= (Button) findViewById(R.id.check);
check.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.check:
//Do the Task Search Here
Intent i = new Intent(getApplicationContext(), Sample.class);
startActivity(i);
break;
}
}
}
Code for Sample
public class Sample extends Activity implements OnClickListener {
Button redirect;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_result);
redirect= (Button)findViewById(R.id.redirect);
redirect.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.redirect:
Here What should i do to get view of Tab2,By default the DemoTab's Current Tab will be Tab1.
Intent i = new Intent(getApplicationContext(), DemoTab.class);
startActivity(i);
break;
default:
break;
}
}
}
Just try like this...
when call the tabactivity call like this
Intent go=new Intent(this,TabActivity.class);
Bundle b=new Bundle();
b.putString("condition","tab2");
go.putExtras(b);
startActivity(go);
then in tabAcitivity class
public class Demo extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle b=getIntent().getExtras();
String con=b.getString("condition");
TabHost tab = getTabHost();
TabSpec tab1 = (TabSpec) tab.newTabSpec("tb1");
TabSpec tab2 = (TabSpec) tab.newTabSpec("tb2");
tab1.setIndicator("Tab1").setContent(
new Intent(this, Tab1.class));
tab2.setIndicator("Tab2").setContent(
new Intent(this, Tab2.class));
tab.addTab(tab1);
tab.addTab(tab2);
if(con.equals("tab2")
{
tab.setCurrentTab(1);
}
}
}
I have two Classes "One.Java" and "Two.Java". My main Activity which is extends 'TabActivity'. In onCreate() method :
Intent one = new Intent(this, One.class);
Intent two = new Intent(this, Two.class);
TabHost t = (TabHost) findViewById(R.id.tabhost);
t.setup();
TabSpec tspecOne = t.newTabSpec("CLICK ONE").setIndicator("CLICK ONE")
.setContent(one);
t.addTab(tspecOne);
TabSpec tspecTwo = t.newTabSpec("CLICK TWO")
.setIndicator("CLICK TWO").setContent(two);
t.addTab(tspecTwo);
setCurrentTab(index) opens the tab to be displayed by default, specified by the index position of the tab. If you set tabHost.setCurrentTab(1); property while creating your TabActivity, it will show the tab on 1st index as you desire( 0 will set it to a tab on 0th index). You might want to check some of the above questions as well:
android setting default tab in tab-activity
Start Android App in a specific tab
setCurrentTab of a tabHost
Edit
Android TabHost setCurrentTab()
public class DemoTab extends TabActivity {
//Declare this outside the elements
TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
//...
}
private void setTabs()
{
// ...
addTab("Tab1", R.drawable.tab_search, Tab1.class);
addTab("Tab2", R.drawable.tab_home, Tab2.class);
// Here you may try adding
tabHost.setCurrentTab(1);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
tabHost = getTabHost(); v//edit this line
//....
}
Hope this helps
how to make the method onWindowFocusChanged(boolean) to true(or call) in one tabs of tablayout in android..
Explanation:
In tablayout onWindowFocusChanged() called automatically in default activity (true), But when we click/touch on to the next tab ( which call another activity)
not able to call the onWindowFocusChanged() !!!!!! how to call onWindowFocusChanged() in second tab?
source code:
public class TabTestActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, TabOne.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("TabOne").setIndicator("TabOne",
res.getDrawable(R.drawable.ic_tab_One))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, TabTwo.class);
spec = tabHost.newTabSpec("TabTwo").setIndicator("TabTwo",
res.getDrawable(R.drawable.ic_tab_az))
.setContent(intent);
tabHost.addTab(spec);
//tabHost.setCurrentTab(2);
}
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
Toast.makeText(this, ""+hasFocus , Toast.LENGTH_LONG).show();
super.onWindowFocusChanged(hasFocus);
}
}
-----------------------------------------------------------------------------------------------------
public class TabOne extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Coll tab");
setContentView(textview);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
Toast.makeText(this, "On window One"+hasFocus , Toast.LENGTH_LONG).show();
super.onWindowFocusChanged(hasFocus);
}
}
--------------------------------------------------------------------------
public class TabTwo extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Coll tab");
setContentView(textview);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
Toast.makeText(this, "On window TabTwo"+hasFocus , Toast.LENGTH_LONG).show();
super.onWindowFocusChanged(hasFocus);
}
}
TabTwo's (2ndTab) onWindowFocusChanged not called while TabTwo activity runs please give the solution.
I tried to provide tabHost.setFocusable(true); I doesn't worked!!!
Tabs are basically deprecated. You should use fragments instead. This will clearly handle your issue.
See: https://stackoverflow.com/questions/9714650/converting-tabactivity-into-fragmentactivity for an example of how to do this.
I have created tab app.. now I am playing with screen rotation. I tried to set tabHost.getTabWidget().setCurrentTab(1), which should show second tab (first is 0). The point is that second tab is shown as selected, but shown content is from first tab... How can I solve that?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
myCommunicator = new Communicator();
dbAdapter = new ToDoDBAdapter(this);
if (getLastNonConfigurationInstance() != null)
{
CurrentTab = (Integer)getLastNonConfigurationInstance();
createView();
}
else
{
BuildDialog = ProgressDialog.show(this, "Loading", "Updating data...", true, false);
BuildDialog.show();
new LoadChannels().execute();
}
}
private void createView()
{
tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setOnTabChangedListener(this);
Intent intent;
TabSpec spec;
intent = new Intent().setClass(this, Channels.class);
// TAB 1
spec = tabHost.newTabSpec("kanali").setIndicator("Kanali",getResources().getDrawable(R.drawable.menu_channels)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Currently.class);
// TAB 2
spec = tabHost.newTabSpec("trenutno").setIndicator("Trenutno",getResources().getDrawable(R.drawable.menu_current)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Reminders.class);
// TAB 3
spec = tabHost.newTabSpec("opomniki").setIndicator("Opomniki",getResources().getDrawable(R.drawable.menu_reminder)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, About.class);
// TAB 4
spec = tabHost.newTabSpec("oprogramu").setIndicator("O programu",getResources().getDrawable(R.drawable.menu_about)).setContent(intent);
tabHost.addTab(spec);
tabHost.setBackgroundColor(Color.WHITE);
tabHost.setCurrentTab(1); // Should always set content to second
}
#Override
public Object onRetainNonConfigurationInstance()
{
return CurrentTab;
}
#Override
public void onTabChanged(String tabId) {
CurrentTab = tabHost.getCurrentTab();
}
public void onDestroy() {
super.onDestroy();
// Close the database
try {
dbAdapter.close();
} catch (Exception e) {
// TODO: handle exception
}
}
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
createView();
}
Why are you calling getTabWidget()? You should use the setCurrentTab() on the tabHost itself.
Works fine here.
tabHost.setCurrentTab(1);
Use setCurrentTabByTag(String tag); Depending upon the tag you mentioned would be the default tab, hope it helps, works (It's currectly working for this code!!)
private static final String A ="Kanali"; //required defualt tab name
.
.
.
tabHost.setCurrentTabByTag (A);
link
Did you check TabHost in the debugger to be sure that all of the tabs were successfully added before you tried to set the currentTab? I had a similar issue and found that TabHost.setCurrentTab remains set to -1 if you try to reference an index that is outside of the range of tabs that were not successfully added. This behavior is not documented in the Android documentation.