I am writing a tab using SomeActivity extends TabActivity.
Here is the main process:
The main activity will call the SomeActivity. And the SomeActivity will put two activity (activityone and activitytwo) to the two tabs.
Here is my question, when the subactivity of the SomeActivity has finished, how could I pass the intent with some extra to the mainactivity?
Here is the main code of SomeActivity, which will be called by the MainActivity.
public class SomeActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(Activity.RESULT_OK);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabSpec spec;
Intent intent; // Reusable Intent for each tab
//TAB1
intent = new Intent(this,ActivityOne.class);
spec = tabHost.newTabSpec("tab1")
.setIndicator("Tab1", res.getDrawable(android.R.drawable.ic_media_play))
.setContent(intent);//
tabHost.addTab(spec);//
//TAB2
intent = new Intent(this,ActivityTwo.class);
spec = tabHost.newTabSpec("tab2")
.setIndicator("Tab1", res.getDrawable(android.R.drawable.ic_media_play))
.setContent(intent);//
tabHost.addTab(spec);//
tabHost.setCurrentTab(0);
}
Here is where I want to send an intent to the MainActivity.
public class ActivityOne extends Activity{
private void SendIntent()
{
Intent intent = new Intent();
intent.putExtra("information", "someinformation");
// Set result and finish this Activity
setResult(Activity.RESULT_OK, intent);
finish();
}
}
Related
i am developing an application where in i am displaying a tabbed layout on click of a button. now i want all my tabs to be scrollable. any help would be appreciated.
Here is my code
AndroidTabLayoutActivity class :
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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, MyInfo.class);
spec = tabHost.newTabSpec("MyInfoTab").setIndicator("MyInfo",
res.getDrawable(R.drawable.myinfo))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Notes.class);
spec = tabHost.newTabSpec("NotesTab").setIndicator("Notes",
res.getDrawable(R.drawable.notes))
.setContent(intent);
tabHost.addTab(spec);
}
}
MyInfo.class
public class MyInfo extends Activity {
ScrollView scroll;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myinfo);
}
}
Notes.class
public class Notes extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notes);
// TODO Auto-generated method stub
}
}
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've been having this problem for 1 week.
I have a MainActivity extends TabActivity and A, B two activities in each tab.
Now I want to press a button in activity A to set the current page to the tab of activity B
But I can't use setCurrentTab method except in MainActivity.
How can I make this function work?
public class MainActivity extends TabActivity
public TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, StartActivity.class);
spec = tabHost.newTabSpec("tab1").setIndicator("A",res.getDrawable(R.drawable.start))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, WeatherActivity.class);
spec = tabHost.newTabSpec("tab2").setIndicator("B",res.getDrawable(R.drawable.weather))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
public class AActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_layout);
Button b = (button)findViewById(R.id.bt);
b.setOnClickListener(this);
}
public void onClick(View v) {
tabHost.setCurrentTab(1);
//Can't work here
}
}
Try making the following changes :
In Main activity :
Make the tabHost variable Static.
private static TabHost tabHost;
Add a new function to fetch the current tabhost.
public static TabHost getCurrentTabHost(){
return tabHost;
}
In AActivity, Use like this :
MainActivity.getCurrentTabHost().setCurrentTab(1);
If required, you can go for a null check.
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.