How to make the back button switch between tabs - android

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.

Related

Returning to previous tab from another activity in android app

I have this android app in which my main activity HomeActivity holds two fragments using a ViewPager and a tab layout. Each fragment holds a list of items and when clicked takes the user to another activity. Now I would like to return to the previous tab layout when the back button is pressed in the current activity. Simply put how do I get back to the tab layout from which the user had come from. Normally a back press would take the user to a default tab, in this case the BtcFragment tab but I would like to be able to return the user to the originally clicked tab in the HomeActivity.
in your Main Activity or where you define your view pager create a static method as below :
public static void ViewPagerTabSelector(int tabNumber) {
mViewPager.setCurrentItem(tabNumber);
}
then from activity you want to back in your `onbackpress call:
#Override
public void onBackPressed() {
MainActivity.ViewPagerTabSelector(0);
}

Android TablaLayout and NavigationDrawer

I have a two problems and struggle for a few days. I am not sure what the right way is.
I want to combine a tab layout with a view pager containing three fragments (the tab layout and the view pager are the home screen) and a navigation drawer.
And I think that everything you can navigate to from the drawer should be a fragment accept settings or something similar.
So should I let the tab layout in the layout of the main activities layout or should I put the this layout also in a fragment (so view pager with fragments inside a so calling home fragment)?
When starting the app I check if the user is logged in and if he is download data from Firebase. I do this in another fragment (data fragment) there I setretaininstance to true.
So currently one of the tab fragments needs the data from Firebase but soon all of them will need it.
How do I pass the data (Lists) from the data fragment to the tab fragments or the so called home fragment and then to the tabs.
I already tried interfaces but I don't know how to handle confirmation changes.
Considering the two problems or the combination of both how would you solve this / which design approach is better only use fragments or let the home screen inside the main activity and change the view when the user navigates from the drawer to fragment?
PS. I will add some code as soon as possible.
Thanks.
TabLayout and the ViewPager should be in the Activity layout since it makes more sense, as the Activity manages the Fragments. Also, navigation drawer should navigates through Activities, and Activities between Fragments. Again, that's the logical thing for a decent code organization, and it prevents repetitions in layout (AKA more than one button to navigate to the same fragment).
For the Firebase issue, I suggest you start the call in the Activity, and then call a function in all fragments with the retrieved information. Something like this:
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(MyFragment f : mFragmentList)
f.addData(dataSnapshot);
}
}
If you really want the Fragment to make the call to Firebase, then you can create you own Event and Listener set.
public class SnapshotRetrievedEvent {
private static List<SnapshotRetrievedListener> listeners = new ArrayList<>();
public static void addListener(SnapshotRetrievedListener listener){
listeners.add(listener);
}
public static void notify(DataSnapshot snapshot) {
for(SnapshotRetrievedListener listener : listeners)
listener.onSnapshotRetrived( snapshot );
}
}
public interface SnapshotRetrievedListene {
void onSnapshotRetrieved(DataSnapshot snapshot);
}
Then in your Fragment waiting for the snapshot, you add the listener like this:
SnapshotRetrievedEvent.addListener(
new SnapshotRetrievedListener(){
#Override
public void onSnapshotRetrieved(DataSnapshot snapshot){
// Do stuff
}
}
);
onSnapshotRetrieved will be called when you call SnapshotRetrievedEvent.notify(snapshot) in your other Fragment.
Hope this helps

How to clear the Child activities on clicking the particular tab

I want to know how to clear child activities and how to invoke the parent activity of a particular tab on its second click (tab functionality like on iPhone?). I want to invoke the click listener for each tab. Assume as if the application gets deeper while using, it's hard to use the back button often to reach the parent activity. So i need to invoke the parent activity by clicking the tab.
On first tab it is working fine, here is the code
getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (getTabHost().getCurrentTabTag().equals("gebrauchte")) {
Intent intent = new Intent(getApplicationContext(),TabHome.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else{
tabs.setCurrentTab(0);
}
}
});
I tried to use the same code for the second tab with getChildAt(1), should i give TabHome class for all intent, if I try to give their respective class of 2nd, 3rd and 4th tab, the tabHost gets hidden.
You don't need to set onClickListener on Tabs. I would advice to use Android Tabs with Fragments

Tab issue in android with back button

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.

android go back to a specific tab

ShareMarketActivity.java this handles the tabs, Tab1, Tab2, Tab3.java files..... which on load it load the 1st tab
in Tab3 (portfolio) when u click on the company it moves to another window, what i want to do is,when u click back button on the new window... it should again load the tabs and it moves to the Tab3.
back.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent("android.intent.action.ShareMarketActivity"));
}
});
So this will load the tabs again and show the tab1 as the starting tab. Not the Tab3. what i want is... ** load the ShateMarketActivity and straight away moves to tab3 ** ( in the 1st load i want Tab1 to be the 1st load not tab3)
You can use the setCurrentTab method of the tab host.
mTabHost.setCurrentTab(2);
What you need to do is listed briefly below. I am not putting any code though but it should be straightforward.
When you are navigating to a new window do not navigate through the child activities that reside in the TAB Host. Call getParent() and then start new Activity from the TabActivity which is the Parent.
Make the Tab launchmode in manifest as SingleTask.
You can not capture the result of the Activity in onActivityResult in TabHost and then call the child Activity to propogate the result by using getCurrentActivity()
The reason for the behavior you see is that Tabs are actually an ActivityGroup with multiple activities embedded in them for each TAB. Once you call a new Activity from any of the child Activity and then come back the Activity stack loses the Activity Group and lists the calling activity as parent and hence you just see TAB3 activity as the only activity.
Let me know if this does not help.

Categories

Resources