I have a screen with 2 tabs in at the bottom of the screen.
Once I run the app, automatically first tab will be active and the activity related to that screen will be shown (Screen 1). Then when the user clicks on the second tab it opens another activity (Screen 2).
Requirement: I want to show the screen 0 when the app loads instead of showing the screen 1. At this time both the tabs should be inactive(not pressed). Once the user clicks on any tab only the respective screens should open (Screen 1 and screen 2).
How to do it?
The code I used is given below:
public class TabBarActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
// Your Tab Titles
String tab_title[] = { "Screen 1", "Screen 2"};
// Your Tab Drawables for their states
int tab_drawables[] = { R.drawable.tab_home_custom,
R.drawable.tab_balance_custom};
// Your Tab Activities
Object tab_act[] = { TabB.class,TabC.class};
// / TabHost setup
final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
TabSpec tab_spec ;
for (int i = 0; i < tab_act.length; i++) {
tab_spec = tabHost.newTabSpec(tab_title[i]);
tab_spec.setIndicator(tab_title[i],
getResources().getDrawable(tab_drawables[i]));
tab_spec.setContent(new Intent(this, (Class<?>) tab_act[i]));
tabHost.addTab(tab_spec);
}
tabHost.setCurrentTab(0);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
TabBarActivity.this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Just create a new activity, screen 0, and get it to start your tab activity, screen 1 and screen 2, using intents. You can then change you manifest file so that you have your new activity marked as launcher by adding this code to the activity decleration.
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
You will also need to remove that code from your current tab activity.
This is not something the TabActivity would normally support.
You might be able to achieve the desired effect by adding a "dummy" tab:
public class TabBarActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
// Your Tab Titles
String tab_title[] = { "", "Screen 1", "Screen 2"};
// Your Tab Drawables for their states
int tab_drawables[] = { R.drawable.tab_home_custom,
R.drawable.tab_balance_custom};
// Your Tab Activities
Object tab_act[] = {Activity.class, TabB.class,TabC.class};
// / TabHost setup
final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
TabSpec tab_spec ;
for (int i = 0; i < tab_act.length; i++) {
tab_spec = tabHost.newTabSpec(tab_title[i]);
if(i>0) tab_spec.setIndicator(tab_title[i],
getResources().getDrawable(tab_drawables[i]));
tab_spec.setContent(new Intent(this, (Class<?>) tab_act[i]));
tabHost.addTab(tab_spec);
}
tabHost.setCurrentTab(0);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
TabBarActivity.this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I'm not sure if this will create a blank tab button in the tab bar though
I suppose you want a splash screen with the app logo,you need to create another activity and navigate to the tabActivity from that
public class splash2 extends Activity{
protected boolean _active = true;
protected int _splashTime = 2000;
Intent intent;
Runnable r;
Handler handler;
/** Called when the activity is first created. */
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_MENU ) {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashlayout2);
// startActivity(new Intent(splash2.this,splash1.class));
// thread for displaying the SplashScreen
handler = new Handler();
r=new Runnable() {
public void run() {
intent = new Intent(splash2.this, TabBarActivity .class);
//intent.setClass();
startActivity(intent);
finish();
}
};
handler.postDelayed(r, _splashTime);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
handler.removeCallbacks(r);
intent = new Intent();
intent.setClass(splash2.this, Main_menu.class);
startActivity(intent);
overridePendingTransition( R.anim.slide_in_left2, R.anim.slide_out_left2 );
finish();
}
return true;
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
}
}
and add the following tag to the activity tag of this activity instead of having those tags inside the TabBarActivity
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
It will make this splash screen the default activity
Related
I am new to android development.I have made an app where the MainActivity consists of a Gridview with images and ActivityTwo is the fullscreen view of the selected image.I am trying to add a back button on the actionbar.The icon is visible but it is not clickable.Also i tried to add other menu items which are also visible but nothing happens on touching them.I have also tried adding onClickListener to the menu items as suggested in some posts but it didn't seem to work.
Below is the ActivityTwo code.
public class ActivityTwo extends ActionBarActivity implements OnClickListener {
protected int currentPosition;
Button share,back;
ImageView imageView;
ViewPager viewPager;
private Toolbar toolbar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
toolbar= (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
share = (Button)findViewById(R.id.button1);
share.setOnClickListener(this);
back=(Button)findViewById(R.id.button2);
back.setOnClickListener(this);
// get intent data
Intent i = getIntent();
// Selected image id
final int position = i.getExtras().getInt("position");
viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(position);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
currentPosition = arg0;
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if
(id==R.id.home)
{
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
public class ImagePagerAdapter extends PagerAdapter {
Integer[] icons = MainActivity.mThumbIds;
#Override
public int getCount() {
return icons.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = ActivityTwo.this;
imageView = new ImageView(context);
//ImageView imageView = (ImageView)findViewById(R.id.imageView);
// int padding = context.getResources().getDimensionPixelSize(
// R.dimen.padding_large);
imageView.setPadding(5, 5, 5, 5);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setImageResource(icons[position]);
imageView.setTag(position);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==share){
ImageView Imgv = (ImageView)viewPager.findViewWithTag(viewPager.getCurrentItem());
Drawable mDrawable = Imgv.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();
String path = Images.Media.insertImage(getContentResolver(),
mBitmap, "Image Description", null);
Uri uri = Uri.parse(path);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share Image")); }
if(v==back)
{
finish();
}
}
}
Try to add this code to your toolbar for navigating back
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
For another item in your toolbar
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(context, "setting", Toast.LENGTH_SHORT).show();
break;
case R.id.action_share:
Toast.makeText(context, "share", Toast.LENGTH_SHORT).show();
break;
}
}
Hope this help
To make your app icon "clickable" as a back button, your Activities must follow a parent-child hierarchy.
Example:
Activity A is your main Activity.
Pressing something (say a ListView item or GridView item) will start Activity B.
In this way Activity A is what's called a parent Activity to Activity B.
You define this in XML in your manifest like so:
<activity
android:name="com.example.android.ActivityA"
android:label="#string/title_activity_a" />
<activity
android:name="com.example.android.ActivityB"
android:label="#string/title_activity_b"
android:parentActivityName="com.example.android.ActivityA">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.ActivityA" />
</activity>
This will make the app icon appear with a back arrow ( < ) to the left of it.
We then must handle the click event.
We so this from within our onOptionsItemSelected method in Activity B like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// Create an intent to start Activity A.
Intent intent = new Intent(ActivityB.this, ActivityA.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
android.R.id.home is the ID of the click event we are listening for.
Sometimes you may wish to simulate a back button press instead of starting Activity A fresh with an intent. You could achieve that like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// Simulate back button press.
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
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 an activity that can be asked to run after clicking buttons on many different activities and hence it does not have a "single parent". Therefore in the android manifest I cannot define its parent so I cant get the "Up" button to function properly.
Is there a way I can have the "up" button return to the activity that called it?
You can pass ComponentName of starting activity as an extra
intent = new Intent(this, UpButtonActivity.class);
intent.putExtra(EXTRA_PARENT_COMPONENT_NAME, new ComponentName(this, ThisActivity.class));
startActivity(intent);
The Activity with up button
private ComponentName parent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parent = getIntent().getParcelable(EXTRA_PARENT_COMPONENT_NAME);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getId()) {
case android.R.id.home:
if (parent != null) {
final Intent parentIntent = new Intent();
parentIntent.setComponentName(parent);
startActivity(parentIntent);
finish();
return true;
} else {
return super.onMenuItemSelected(featureId, item);
}
//...
}
}
I have 3 Tabs
Tab1,Tab2,Tab3
public Class TabLayout extends TabActivity {
Tab1,Tab2,Tab3
}
Each Tab Contains one ListActivity
that means
Tab1>>>Tab1ListActivity(List with Image and Text)
Tab2>>>Tab2ListActivity(List with Image and Text)
Tab3>>>Tab3ListActivity(List with Image and Text)
and if i click on any listitem from(Tab1ListActivity,Tab2ListActivity etc.......)
I'm forwarding to one More Activity(Tab1ListItemActivity,Tab2ListItemActivity etc......)
But My Actual Requirement is if suppose I'm clicking backbutton from Tab2ListItemActivity ,then I
Should be able to forward to Tab2ListActivity
But,if i write my code like this
public class Tab2ListItemActivity extends Activity
{
//on Back Button Pressed
Intent intent=new Intent(Tab2ListItemActivity.this,TabLayout.class);
startActivity(intent);
Tab2ListItemActivity.this.finish();
}
when i execute above code I'm able to forward to my TabLayout(Tab1ListActivity) but not to Tab2ListActivity?,Beacuse
Tab2ListItemActivity related to Tab2ListActivity
Could any one help?
Why not leave the default behaviour (do not override onBackPressed)? It will restore to previous Activity.
Solution 1:
Do not finish the TabLayout Activity. And Leave the default implementation of back button in Tab2ListItemActivity activity.
Solution 2 (the hard, the force way):
Add the intent extra on what tab it should open.
Intent intent = new Intent(Tab2ListItemActivity.this, TabLayout.class);
intent.putExtra(TabLayout.EXTRA_TAB_TO_OPEN, 1);
startActivity(intent);
Tab2ListItemActivity.this.finish();
in TabLayout Activity
#Override
protected void onPostCreate(Bundle state) {
super.onPostCreate(state);
handleTab(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleTab(intent);
}
private void handleTab(Intent intent) {
// init tabs
final int tab = intent.getIntExtra(EXTRA_TAB_TO_OPEN, -1);
if (tab != -1) {
getTabHost().setCurrentTab(tab);
}
}
Why are you trying to override the Back button ? I mean, just let the default behaviour take place and then pressing the back button would take you to the previous activity.
If you really want to do it your way, then you would need to set the tab id before launching the TabLayout activity and inside the onCreate() of TabLayout you would need to check the tab id and launch the corresponding ListActivity. The tab id is 1 by default and that is why it is taking you to the Tab1ListActivity always.
I am getting same issue but i solve using this solution.
TabSample.class
public class TabSample extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("OPT")
.setContent(new Intent(this, TabGroup1Activity.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("EDIT")
.setContent(new Intent(this, TabGroup2Activity.class)));
tabHost.setCurrentTab(1);
}
}
TabGroup1Activity.class
public class TabGroup1Activity extends TabGroupActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("OptionsActivity", new Intent(this,OptionsActivity.class));
}
}
OptionsActivity.class
public class OptionsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.optionspage);
Button back = (Button) findViewById(R.id.BackButton1);
Button next = (Button) findViewById(R.id.NextButton1);
OnTouchListener backListener = new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_UP){
finish();
}
return false;
}
};
OnTouchListener nextListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_UP){
Intent edit = new Intent(getParent(), EditActivity.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("EditActivity", edit);
return true;
}
return false;
}
};
back.setOnTouchListener(backListener);
next.setOnTouchListener(nextListener);
}
}
finally over TabGroupActivity.class which save all the stat instance.
public class TabGroupActivity extends ActivityGroup {
private ArrayList<String> mIdList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mIdList == null) mIdList = new ArrayList<String>();
}
#Override
public void finishFromChild(Activity child) {
LocalActivityManager manager = getLocalActivityManager();
int index = mIdList.size()-1;
if (index < 1) {
finish();
return;
}
manager.destroyActivity(mIdList.get(index), true);
mIdList.remove(index); index--;
String lastId = mIdList.get(index);
Intent lastIntent = manager.getActivity(lastId).getIntent();
Window newWindow = manager.startActivity(lastId, lastIntent);
setContentView(newWindow.getDecorView());
}
public void startChildActivity(String Id, Intent intent) {
Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
mIdList.add(Id);
setContentView(window.getDecorView());
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
public void onBackPressed() {
int length = mIdList.size();
if ( length > 1) {
Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
current.finish();
}
}
}
This is awesome solution you can try this.and make appropriate manifest entry.
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();
}
};}