Move back to parent activity from child activity android - android

In my app, I have one tab which includes two activities say 1 and 2. Activity 1 extends ActivityGroup, which has one child activity say A. Now scenario becomes like this,
In first tab --> Activity 1 and its child activity A.
In second tab --> Activity 2. After doing some functionality in Activity 1 of first tab, user moves to child activity A of first tab. Now what I want to do, when user presses back button from child activity A, user should move back to its parent activity 1. How to do that? Below is my code..
MainActivity.java
public class MainActivity extends TabActivity {
TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources ressources = getResources();
tabHost = getTabHost();
Intent intentProfile = new Intent().setClass(this, Tab1.class);
TabSpec tabProfile = tabHost
.newTabSpec("Profile")
.setIndicator("Profile", ressources.getDrawable(R.drawable.ic_launcher))
.setContent(intentProfile);
Intent intentFriends = new Intent().setClass(this, Tab2.class);
TabSpec tabFriends = tabHost
.newTabSpec("Friends")
.setIndicator("Friends", ressources.getDrawable(R.drawable.ic_launcher))
.setContent(intentFriends);
tabHost.addTab(tabProfile);
tabHost.addTab(tabFriends);
tabHost.setCurrentTab(0);
}
public void switchTabBar(int tab) {
tabHost.setCurrentTab(tab);
}
#Override
public void onBackPressed() {
// Called by children
MainActivity.this.finish();
}
}
Tab1.java
public class Tab1 extends ActivityGroup {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(Tab1.this, Second.class);
replaceContentView("activity3", in);
}
});
}
public void replaceContentView(String id, Intent newIntent)
{
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}
#Override
public void onBackPressed() {
this.getParent().onBackPressed();
}
}
Second.java
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
#Override
public void onBackPressed() {
this.getParent().onBackPressed();
MainActivity parentTab = (MainActivity) this.getParent();
parentTab.switchTabBar(0);
}
}
Tab2.java
public class Tab2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
}
#Override
public void onBackPressed() {
// this.getParent().onBackPressed();
MainActivity parentTab = (MainActivity) this.getParent();
parentTab.switchTabBar(0);
}
}
Here I can move to tab 1 from tab 2 on back button pressed. But, I can't move back to parent activity from child activity of tab 1.

You have to override back button in parent activity and make a call from child. For more info please check this link : Navigate Between Activities in an ActivityGroup

In Tab1:
#Override
public void onBackPressed() {
super.onBackPressed();
}
#Override
public void onPause()
{
super.onPause();
finish();
}
in Tab1

Related

how to get back to fragment from activity?

I have four fragments in an Activity C. they are behaving as tabs. I have to go from a fragment to a new Activity X. Now i want to come back to fragment from Activity X to fragment.
here is my main activity
'public class MainInterface extends ActionBarActivity {
ViewPager pager;
PagerTabStrip tab_strp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_interface);
MainPagerAdapter mapager = new MainPagerAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(mapager);
tab_strp = (PagerTabStrip) findViewById(R.id.tab_strip);
//tab_strp.setTextColor(Color.WHITE);
//tab_strp.setTextSize(14,14);
//tab_strp.setTabIndicatorColor(Color.WHITE);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2196f3")));
getSupportActionBar().setTitle("Instructor");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
'
here is activity
'public class Discussions extends Fragment implements View.OnClickListener {
ImageButton post;
TextView dTitle;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.discussions,container,false);
post=(ImageButton)view.findViewById(R.id.ibDisc);
post.setOnClickListener(this);
dTitle=(TextView)view.findViewById(R.id.tvDiscTitle);
return view;
}
#Override
public void onClick(View view) {
Intent in=new Intent(getActivity(),PostDiscussion.class);
startActivity(in);
}
}'
Save the name of your fragment in sharedPreferences before navigating to new activity and onBackpressed of that new activity or when you want to come back to same fragment get the name from SharedPreferences and add that particular fragment to the earlier activity

How to Show Second Tab in Android while calling from an Activity

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

Problems with OnClickListener for a tab in android

I'm new to android programing and I'm having problems with OnClickListener for tabs in my app. I found on stack a solution how it should be done, but for some reason it's not working.
I'm trying to use the 2nd answer
For some reason I'm getting 2 errors.
First one is on the name on of my activity: The type DragonLords must implement the inherited abstract method View.OnClickListener.onClick(View).
Second one is on the OnClick method: The method onClick(View) of type new View.OnClickListener(){} must override a superclass method.
Here is a part of my code:
public class DragonLords extends TabActivity implements OnClickListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
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, Home.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("home").setIndicator("home",
res.getDrawable(R.drawable.hometab))
.setContent(intent);
tabHost.addTab(spec);
getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (getTabHost().getCurrentTab()==0) {
getTabHost().setCurrentTab(0);
}else
{
getTabHost().setCurrentTab(0);
}
}
});
After that I'm creating more tabs. With out the onclicklistener it's working, the thing is I need to be able to reload the tabs when they are active.
Anyone have an idea what I'm doing wrong?
I added the necessary imports.
Gatz
You must implement the onClick method not in an anonymous inner class like you have done in your code.
Try using new TabWidget.OnClickListener instead of just the normal OnClickListener
Something akin to the following:
public class TestActivity extends TabActivity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getTabWidget().getChildAt(0).setOnClickListener(new TabWidget.OnClickListener() {
public void onClick(View v) {
if (getTabHost().getCurrentTab()==0) {
getTabHost().setCurrentTab(0);
}else
{
getTabHost().setCurrentTab(0);
}
}
});
}
public void onClick(View theView) {
// Do something with view here
}
}
you are doing it pretty hard way.. i did it like this...
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tablayout);
res = getResources();
tabHost=(TabHost)this.findViewById(android.R.id.tabhost);
tabHost.getTabWidget().setDividerDrawable(R.drawable.vertical_seperator);
setupTab(new TextView(this), "Login",new Intent().setClass(this,loginForm.class));
setupTab(new TextView(this), "Can't Login",new Intent().setClass(this,ForgotPwd.class));
setupTab(new TextView(this), "Register",new Intent().setClass(this,RegisterUser.class));
}
private void setupTab(final View view, final String tag,final Intent myIntent)
{
View tabview = createTabView(tabHost.getContext(), tag);
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(
new TabContentFactory()
{
public View createTabContent(String tag)
{return view;}
}).setContent(myIntent);
tabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text)
{
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
hope this helps....
tabs_bg is just an xml with
<TextView android:id="#+id/tabsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="15dip"
android:textColor="#drawable/tab_text_selector" />

change TabActivity title dynamically according to selected activity tab data

i have created MyTabActivity extending TabActivity class with two tabs.
public class MyTabActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("My App Name");
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec(TAB_1).setIndicator(TAB_1, getResources().getDrawable(R.drawable.tab1)).setContent(new Intent(this, first.class)));
tabHost.addTab(tabHost.newTabSpec(TAB_2).setIndicator(TAB_2, getResources().getDrawable(R.drawable.tab2)).setContent(new Intent(this, second.class)));
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
}
});
tabHost.setCurrentTabByTag(TAB_1);
}
}
i need to change the title of MyTabActivity according to change in one of activity data.
public class first extends Activity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
//change the MyTabActivity title from here ???
}
}
Please help me how to do this.
Thanks
You can get your TabActivity inside child Activity like this.
If you are in FirstActivity or SecondActivity try something like this.
MyTabActivity myTabs = (MyTabActivity) this.getParent();
myTabs.setTitle("This is First cool Activity");
you can do the same in SecondActivity.
Cheers!!

When switching tabs in Android, what does it constitute?

I have three tabs and each one is its own activity. When I switch tabs I want my Spinner to update but I don't know what method gets called on tab switch. Any help?
Hook up a listener to the TabHost.OnTabChangeListener. You main activity extends TabActivity and its onCreate method probably looks something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("tab1")
.setContent(R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab2")
.setContent(R.id.view2));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab3")
.setContent(R.id.view3));
}
To hook up the listener, add the following code in your onCreate() method:
tabHostt.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
if(tabId.equals("tab1")) {
//tab1
}
else if(tabId.equals("tab2")) {
//tab2
}
else if(tabId.equals("tab3")) {
//tab3
}
}
});
HTH
Something like this should work:
tabHost.setOnTabChangedListener(new OnTabChangeListener()
{
public void onTabChanged(String tabId)
{
//Do stuff in here
}
});

Categories

Resources