I have created a TabActivity, which has two tabs and each of the tabs displays separate activities. I am unable to call method onConfigurationChanged, defined in both of these activities, when a user changes the tabs. Here is the code:
public class MyTabActivity extends TabActivity implements OnTabChangeListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_page);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec = null;
Intent intent = null;
tabHost.setOnTabChangedListener(this);
// ACTIVITY 1
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Activity1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("act1").setIndicator("ACT1",
res.getDrawable(R.drawable.tab_home))
.setContent(intent);
tabHost.addTab(spec);
// ACTIVITY 2
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Activity2.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("act2").setIndicator("ACT2",
res.getDrawable(R.drawable.tab_home))
.setContent(intent);
tabHost.addTab(spec);
}
#Override
public void onTabChanged(String arg0) {
Log.d("DGTabActivity", "tab changed");
if (tabHost.getCurrentTab() == 0) {
// How to call method on Act1??
callOnConfigurationChanged("com.tab.Activity1"); // doesn't work
}
else if (tabHost.getCurrentTab() == 1) {
// How to call method on Act2??
}
}
I also tried to use the following code to call the method onConfigurationChanged, within the onTabChanged method, but it throws exception:
private void callOnConfigurationChanged (String className) {
try{
//onConfigurationChanged
//Class amnClass = Class.forName("android.app.ActivityManagerNative");
Class amnClass = Class.forName(className);
Object amn = null;
Configuration config = null;
Resources res = null;
// res = amn.getResources();
Method methodGetConfiguration = amnClass.getMethod("getResources");
methodGetConfiguration.setAccessible(true);
res = (Resources) methodGetConfiguration.invoke(amn);
config = res.getConfiguration();
// amn.onConfigurationChanged(config);
Method methodOnConfigurationChanged = amnClass.getMethod("onConfigurationChanged");
methodOnConfigurationChanged.setAccessible(true);
methodOnConfigurationChanged.invoke(amn, config);
}
catch (Exception e) {
Log.d("Tab host Exception" , e.toString());
}
}
Thanks for your time.
cheers
Ani
You can use intent filters. Broadcast configuration-changed
event in your tabs' activity and add listeners to activities in your tabs.
Related
I have created two tabs.Tab A and Tab B. From Tab A, i am navigating to new activity say C. From C how to navigate to Tab A of particular activity. When i tried, it's opening new activity and I can't call which is present inside the tab.Please help me.
public class TabsActivity extends TabActivity {
String Activity;
Intent i;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
TabHost tabHost = getTabHost();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Tab A");
// setting Title and Icon for the Tab
photospec.setIndicator("Tab A");
Intent photosIntent = new Intent(this, A.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Tab B");
songspec.setIndicator("Tab B");
Intent songsIntent = new Intent(this, B.class);
songspec.setContent(songsIntent);
// Tab for Videos
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
}
In A activity using button, i am moving to another activity..say for eg: NewActivity
In NewActivity, how to switch back to A which is present inside Tabs.
NewActivity.java
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(ListActivity.this,
listsearchbutton = (Button) findViewById(R.id.listfilter);
listsearchbutton.setOnClickListener(new View.OnClickListener() {
#Override
TabsActivity.class);
intent.putExtra("Activity", "A");
//i dont know where to use getIntent in TabsActivity to call A activity
startActivity(intent);
}
});
Thanks in advance.
Just use Activityname.this.finish(); on button click
I have class CustomTab extends TabActivity:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_tab);
tabHost = getTabHost();
Intent intentA = new Intent(getBaseContext(), A.class);
Intent intentB = new Intent(getBaseContext(), B.class);
Intent intentC = new Intent(getBaseContext(), C.class);
TabSpec tabA = tabHost
.newTabSpec("a")
.setIndicator("",
getResources().getDrawable(R.drawable.icon_a))
.setContent(intentA);
TabSpec tabB = tabHost
.newTabSpec("b")
.setIndicator(
"",
getResources()
.getDrawable(R.drawable.icon_b))
.setContent(intentB);
TabSpec tabC = tabHost
.newTabSpec("c")
.setIndicator("",
getResources().getDrawable(R.drawable.icon_c))
.setContent(intentC);
tabHost.addTab(tabA);
tabHost.addTab(tabB);
tabHost.addTab(tabC);
tabHost.setCurrentTab(0);
and activity B:
Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
((CustomTab) getParent()).getTabHost().setCurrentTab(3);
I want switch to tab 3(activity C) when run activity B so I tried with "((CustomTab) getParent()).getTabHost().setCurrentTab(3);" it only change tab, but content not change, I created a topic similar but not receive correct answer.
I managed this things calling TabActivity again.
In TabActivity
int tabNumber = getIntent().getExtras().getInt("tabNumber");
tabHost.setCurrentTab(tabNumber);
And calling TabActivity in child Activity like your Activity B like this,
Intent intent = new Intent(BActivity.this,
CustomTab.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("tabNumber",3);
startActivity(intent);
Dont no its correct solution for this or not. but I havn't got any other solution.
So, i used this.
I hope you can help, I can't seem to get the current tab to reload if a tab is currently selected and then a user click on it again, could someone point out what I'm doing and perhaps modify my code to show me what I'm doing wrong as I have viewed a lot of threads on here and via google but now one seems to know the answer, that or I'm just dumb :D thank you :)
public class HelloTabWidget extends TabActivity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
final TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
// Log.d(debugTag, "onTabChanged: tab number=" + mTabHost.getCurrentTab());
switch (tabHost.getCurrentTab()) {
case 0:
//do what you want when tab 0 is selected
test();
break;
case 1:
//do what you want when tab 1 is selected
break;
default:
break;
}
}
});
}
public void test (){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Warning");
alert.setMessage("You are about to self-destruct!");
alert.show();
}
First create one variable in application class like below:
public class GlobalClass extends Application {
public int displayTab = 0;
public int getDisplayTab() {
return displayTab;
}
public void setDisplayTab(int displayTab) {
this.displayTab = displayTab;
}
}
and modify your Activity as below:
public class HelloTabWidget extends TabActivity implements OnClickListener {
GlobalClass globel;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
globel = (GlobalClass) getApplication();
Resources res = getResources(); // Resource object to get Drawables
final TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(globel.getDisplayTab());
//Here Handling first tab click
getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
test();
Log.d("Clickedddddddddddd", "1"
+ getTabHost().getCurrentTabTag());
HelloTabWidget.this.finish();
globel.setDisplayTab(0);
Intent i = new Intent(HelloTabWidget.this, HelloTabWidget.class);
startActivity(i);
}
});
}
public void test (){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Warning");
alert.setMessage("You are about to self-destruct!");
alert.show();
}
}
i want to make an intent from a tab to another.. how can i do that?
i only now how to make between activity's. and i need to go from a tab to another..
i have this code for the tabs
public class Main extends TabActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
//intent = new Intent().setClass(this, Contas.class);
Intent a = new Intent(Main.this, Contas.class);
Intent b = new Intent(Main.this, Registros.class);
Intent c = new Intent(Main.this, Relatorios.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Contas").setIndicator("Contas",
res.getDrawable(R.drawable.ic_tab_accounts))
.setContent(a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);
// Do the same for the other tabs
//intent = new Intent().setClass(this, Registros.class);
spec = tabHost.newTabSpec("Registros").setIndicator("Registros",
res.getDrawable(R.drawable.ic_tab_registry))
.setContent(b.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);
//intent = new Intent().setClass(this, Relatorios.class);
spec = tabHost.newTabSpec("Relatorios").setIndicator("Relatorios",
res.getDrawable(R.drawable.ic_tab_reports))
.setContent(c.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
Rather then calling intent you can just set the desired tab selected.
tabHost.setCurrentTab(index)
Rather then calling intent you can just set the desired tab selected.
Use
intent.putExtra("tabIndex", index);
and call the activity. Now in the calling Activity's onCreate() or onResume() use
int index = getIntent().getExtraInt("tabIndex", -1);
if(index != -1)
tabHost.setCurrentTab(index);
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.