Tab issue in android with back button - android

I am using tab host(tabs) in application, it have four tabs on home screen, I have set new layouts on every tabs, I want to that once user clicking tabs like: tab1>tab2>tab3>tab4 and press back on tab4 he/she should go back to tab3 and on tab3 pressing back go to tab2 and so on. I didn't get any idea, how to implement? Could any one help me on this, any help will be appreciated. Thanking you all..!!

Create the below method in your class which extends TabActivity,
public void switchTabBar(int tab) {
tabHost.setCurrentTab(tab);
}
And now in your activity's back press method, do this,
public void onBackPressed() {
TabBar parentTab = (TabBar) this.getParent();
parentTab.switchTabBar(1);//Instead of 1 provide the tab position which you want to navigate to.
}

you can override onBackPressed() and inside it do what you want.

I dont know why you want to add back button.In tabs there's no need to add back button.See the below link
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

You need to create some sort of a activities-backstack (if you use nested ActivityGroup), override onBackPressed() method in your TabActivity and manage this backstack in onBackPressed() method.

You have 2 options:- with Tab or without Tab.
If it is not necessary that you have to use tab in your code then instead of tab you can use buttons and you can easily get your goal. But if you want to use tab then you have to Override onBackPressed() in every 4 Activity.
I hope you got any idea according to my answer. I hope it helps you.

Related

How to update a tab layout before switching to another tabs

I'm working on a project that has tab fragments in android. They are A,B,C and D.So the problem comes when I click tab B there is a button called "like" when this button direct to another activity. There we can like or unlike friends. When the back button clicked on that activity the tab B fragment should be updated automatically.
I referred so much in the internet but couldn't find a suitable answer to me.
I'm very new to android so I'm asking for your help thanks..
I use this code onResume
public void onResume()
{ // After a pause OR at startup
super.onResume();
setbList(bLikedList);
getBLiked();
bListAdapter = new bListAdapter(getActivity(), bLikedList);
listView.setAdapter(bListAdapter);
//Refresh your stuff here
}
Did you try SetCurrentItem(int i) for ViewPager on Call of OnResume()

How to use setContentView(layout_id) second time in main_activity of android code?

My activity_main.xml has 2 buttons. I have implemented onClickListner(); for both of them .
For MainActivity, - > setContentView(R.layout.activity_main)
This activity_main has the 2 buttons.
Button1 - setContentView(R.layout.layout1);
Button2 - setContentView (R.layout.layout2);
Is this the proper way to use?? Because..
The program runs fine. The problem is that when I click Button2, the layout2 loads, I want to come back to main_activity now, So I press 'Back' button.
The entire app closes..!! I am taken to homescreen of the phone.
How to get around this?? I say ViewSwitcher. I do not have a button to come back.
Any other way?? Please excuse for basic question and bad English.
U have to override onBackPressed() function in your activity and again u have to setContentView to activity_main.xml and remove the super.onBackPressed from onBackPressed.
Something like this:
#Override
public void onBackPressed() {
//validation if you are in second layout
if(layout2){
//do things
showLayout1();
}else{
super.onBackPressed();
}
}
You should pretty much never call setContentView() more than once. What you're describing is standard backstack behavior. Either start a new Activity whatever layout you're transitioning to, or switch to using Fragments, and add a new Fragment to the backstack instead of calling setContentView().
Both approaches will give you native behavioral support for the back button.

How to make the back button switch between tabs

I have an application first showing a login screen which on a succesful login changes to a tab view with 3 tabs. Each tab is containing a different activity. When I switch between the tabs I would like the back button to return to the previous tab instead of the login screen.
How should the be done? I'm considering implementing onBackPressed on the tab view and remembering the tab-stack manually but I don't feel that is the right approach.
Let say you have tabs created in class called Main.java. Create there method like this:
public void switchTab(int tab) {
tabHost.setCurrentTab(tab);
}
which will change current tab. Next in activity where you want to change navigation insert the following code:
#Override
public void onBackPressed() {
Main m = (Main)getParent();
m.switchTab(0);
}
This should work.

Android tabs event

I stuck with one problem. actually my screen consists of two tabs. under each tab i have 4-4 activity. i m displaying each activity with the help of activity group in single tab.
Suppose i m in 1st tab which is active. Under this tab i m on 2nd activity(e.g first activity is list activity and second activity gives the result from the first activity)
I want when i click on 1st tab again it should show me the first activity again without using back button.?
I had that problem sometime ago... and that happens because people like to emulate the bottom bar of the iPhone. Android apps don't work that way and using Activity Group is always a signal of a poor UI design.
Anyway, this is what I did:
tabHost.setCurrentTabByTag(TAB_ID_MORE);
tabHost.getCurrentTabView().setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if( MoreGroupActivity.self != null ) {
MoreGroupActivity.self.reset();
}
tabHost.setCurrentTabByTag(TAB_ID_MORE);
}
});
tabHost.setCurrentTabByTag(TAB_ID_HOME);
The above code is not generic, but will give you an idea of the workaround I found. Let me explain:
tabHost.setCurrentTabByTag(TAB_ID_MORE); I use this to select a current tab (in my case, the main tab was another tab, so I had to to this and then change back with tabHost.setCurrentTabByTag(TAB_ID_HOME);). I mean, in my case, the only tab with that behavior was the "More" tab.
tabHost.getCurrentTabView().setOnClickListener this allows you to put a listener to the tab. As you may have already noticed, using OnChangeTabListener is not an option in this kind of situation.
MoreGroupActivity.self Inside my group activity, I had a static field referencing the group activity it self. This kind of hacks are common while using this crappy approach.
tabHost.setCurrentTabByTag(TAB_ID_MORE); this reset the tab so that it can change back to your first activity.
When you are adding a new TabHost.TabSpec to the TabHost
use
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
to the respective Intent

Android:How to change opened tab dynamically

I have an Android application which has four tabs (I use a main TabActivity with TabHost and TabSpecs).
In one of my sub activity (activity opened in a tab), i need to open a tab not by clicking on the tab title and i don't know how to do this.
For example, i have a button in my activity and when i click on it, it opens a different tab.
For the moment, it is what i do:
Intent intent = new Intent(myActivity.this, myTabActivity.class);
intent.putExtra("ComeFrom", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Then in the TabActivity, if i get true reading the "ComeFrom" extra i open the wished tab but the problem is that it kills all the other activies. So, if someone knows a better (cleaner) way to do that trick, please tell me...
Found an easier (I think) answer:
on the TabActivity declare a public, static and self variable and populate it on the onCreate method. F.e.:
public class TheActivity extends TabActivity {
public static TheActivity self;
...
#Override
public void onCreate(Bundle savedInstanceState) {
self=this;
on any Activity running in a tab, when you want to change the one shown on your app. you can do this:
TabHost tabHost = TheActivity.self.getTabHost();
tabHost.setCurrentTab(0);
Worked ok for me, hope serves someone else!
You have to use TabHost's "setCurrentTab(...)" for that. In one of my projects, I created a static method in the main Activity (the one with the TabHost), named "swtichToTab(int tab)". In my subactivites (those inside the tabs) could then just call "MainActivity.switchToTab()" to trigger switching.
It may not be the cleanest method, I'm sure you can achieve this using broadcast intents too.
You can create a BroadcastReceiver and send a broadcast with the index of the tab as extra
You can use views instead of activities for the content of the tabs. This way, the code is simpler and doesn't use as much memory. Plus, you then can use the setCurrentTab(tabIndex) method to easily switch between views.
I have a simple tutorial here. It has a tab activity with a list and map view. When you you click on an item in the list, the activity dynamically goes to the map view (using the setCurrentTab(tabIndex) method). You can easily modify this to have a button switch views.

Categories

Resources