How to change Actionbar Tab from fragment - android

i have 3 actionbar tabs home:video:purchase by default home will be selected.I can switch between different tabs where i'm able to replace the fragments.I have a gridview in home fragment on click of any grid cell i'm using the below piece of code to navigate to purchase tab,i'm able to replace the home fragment with purchase fragment but how can change the actionbar tab from home tab to purchase tab?????
FragmentTransaction ft = parentActivity
.getFragmentManager()
.beginTransaction();
ft.replace(R.id.mainLayout,new purchase());
ft.commit();
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (fragment != null) {
ft.setCustomAnimations(R.animator.fragmentanimatorleft,
R.animator.fragmentanimatorright);
ft.replace(R.id.mainLayout, fragment);
// isSettingClicked = false;
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (fragment != null) {
try {
ft.remove(fragment);
} catch (Exception e) {
// TODO: handle exception
}
}
}

Related

Getting field values from Fragment in onTabSelected

I am trying to set up validation script between tabs (users cant switch from one tab to another without required fields being filled). I am having trouble getting field values from the fragments/Tabs to the onTabSelected method of my TabListener class. As it is I can identify the index of the selected tab and was hoping to use that validate fields of previous tab
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
public class MyTabListener implements ActionBar.TabListener{
private Fragment fragment;
public MyTabListener(Fragment fragment) {
this.fragment = fragment;
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
if(tab.getPosition() == 0)
{
ft.replace(R.id.content, fragment);
}
else if(tab.getPosition() == 1)
{
ft.replace(R.id.content, fragment);
}
else if(tab.getPosition() == 2)
{
ft.replace(R.id.content, fragment);
}
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
Inside your fragment put a function
public static boolean checkValidation() {
/*...check validation for all fields here,
return true if all fields are filled else return false....*/
if(validated){
return true;
}else {
return false;
}
return false;
}
Then inside onTabSelected
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
if(tab.getPosition() == 0)
{
if(YourFragment.checkValidation() == true) {
ft.replace(R.id.content, fragment);
} else{
//do something
}
}
}
I'm not sure if this works. Just try this and let me know if this works. :)

Using unknown number of tabs

I want to create an activity that can display any number of tabs, without creating an activity for each tab.
Is it possible?
The only way I saw creating tabs included creating an activity for each one.
You can try as follows ,
Extend your activity from ActionBarActivity and add required number of tabs programmatically ,
ActionBar.TabListener tabListener;
mactionBar = getActionBar();
mactionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tabListener=new ActionBar.TabListener() {
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
displayTabs(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
displayTabs(tab.getPosition());
}
};
mactionBar.addTab(mactionBar.newTab().setText(<any text>).setTabListener(tabListener));
//call the same method for required number of tabs
Call the same fragment for each tab click
private void displayTabs(int position)
{
Fragment fragment = null;
switch (position) {
case 0:
fragment = new YourFragment();
break;
case 1:
fragment = new YourFragment();
break;
// same for required numder of cases
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(<container id>, fragment).commit();
}
}

How to attach existing fragment

How can I attach existing fragment, when change back tab in ActionBar.NAVIGATION_MODE_TABS?
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.detach(fragment);
}
}
You can verify if your fragment was loaded previously in the fragment stack. Also could store all your used fragments to reuse it
public void setFragment(Fragment fragmentInstance) {
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
Fragment oldFragmentInstance = fragmentManager
.findFragmentById(R.id.curr_fragment);
boolean onlyAttach = false;
List<Fragment> prevFragments = getSupportFragmentManager()
.getFragments();
for (Fragment fragment : prevFragments) {
if (fragment == null) {
continue;
}
if (fragment.getClass().equals(fragmentInstance.getClass())) {
onlyAttach = true;
fragmentInstance = fragment;
break;
}
}
fragmentTransaction.detach(oldFragmentInstance);
if (onlyAttach) {
fragmentTransaction.attach(fragmentInstance);
} else {
fragmentTransaction.replace(R.id.curr_fragment,
fragmentInstance);
fragmentTransaction.addToBackStack(null);
}
try {
fragmentTransaction.commit();
}
}
Hope this helps

FragmentTransaction is not allowed to be added to the backstack

I am trying to add fragments to the backstack but it is not working, I keep getting a: java.lang.IllegalStateException: This FragmentTransaction is not allowed to be added to the back stack.
I am using actionbar sherlock and in my main activity, i listen for the tabs
private class MyTabListener implements ActionBar.TabListener{
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft){
//to hide keyboard
final InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myView.getWindowToken(), 0);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
if(tab.equals(tabData))
{
ft.setCustomAnimations(R.anim.animation_fragment_out, R.anim.animation_fragment_switch);
ft.replace(android.R.id.content, dataFrag);
imm.hideSoftInputFromWindow(myView.getWindowToken(), 0);
//ft.setTransition(R.anim.animation_fragment_switch);
curTab = tabData;
ft.addToBackStack(null);
//ft.commit();
//ft.commit();
}
else if(tab.equals(tabComp))//new competition
{
ft.setCustomAnimations(R.anim.animation_fragment_out, R.anim.animation_fragment_switch);
ft.replace(android.R.id.content, teamsFrag);
//ft.setTransition(R.anim.animation_fragment_switch);
//ft.commit();
actionBar.removeTab(tabData);
actionBar.removeTab(tabComp);
actionBar.addTab(tabMatches);
actionBar.addTab(tabRed);
actionBar.addTab(tabBlue);
curTab = tabComp;
ft.addToBackStack(null);
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft){
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft){
}
}

ActionBar tabs with FragmentActivity

class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(DashboardFragmentActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
Is it possible instead to change Fragment to FragmentActivity? How would you implement this, I'm confuse about those two.
Fragments are always used in FragmentActivities, so you cannot replace a Fragment with a FragmentActivity, because nested activities are deprecated.
And you always have an option to use getActivity(); in the Fragments to get the parent Activity, so for whatever reason you wanted to replace Fragment with FragmentActivity it can be achieved with just Fragment.

Categories

Resources