I have a system of actionbar Tabs with ABS. The first tab is a login tab. I store the logged in user in a local SQLite db and a web SQL db. When the user hits the loginButton and the Async loginTask completes, I’d like to change the login tab’s xml layout.
So basically I can run a test to see if the user is logged in, and if so use a new layout. Is there any reasonable way to complete the layout switch in a SherlockFragment that resides in a system of actionBar tabs?
Obviously I would need to run the same checks inside of onResume as well to make sure if the user is logged in, the right UI shows up. My problem there is a currently inflate my layout inside onCreateView, which doesn’t rerun.
Here’s some code:
public class LoginFragment extends SherlockFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
userFunctions = new UserFunctions();
//test ive made that has no effect since onCreateView only runs once.
if(userFunctions.isUserLoggedIn(activity.getApplicationContext())) {
View v = inflater.inflate(R.layout.loggedin, container, false);
return v;
} else {//standard method when I didnt use the above test
View v = inflater.inflate(R.layout.loginfragment, container, false);
return v;
}
}
I have an activity that hosts the tabs etc, here’s its relevant code:
public static class TabsAdapter extends FragmentPagerAdapter
implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
/*Constructor method that adds a TabsAdapter to each tab that is created.
* It also adds the ViewPager to each tab so that the user can swipe to change tabs.
*/
public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
/*A fairly simple method that sets the TabInfo for each tab so that the TabsAdapter
* knows which class the tab that is being added actually belonds to. It also updates
* the UI interface when each tab is added.
*/
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
public int getCount() {
return mTabs.size();
}
/*A method that is used in other classes to allow each tab Fragment to
* access its inherited methods from a mother-class, in this case, SherlockFragment
*/
public SherlockFragment getItem(int position) {
TabInfo info = mTabs.get(position);
return (SherlockFragment)Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
/*This method reads the user's selection for a new tab and sets that tab as
* the new current focus.*/
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
/* This is the method that actually draws the newest tab onto the screen when
* it is selected.*/
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
Object tag = tab.getTag();
for (int i=0; i<mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
public void onTabReselected(Tab tab, FragmentTransaction ft) {}
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {}
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {}
}
Inside your fragment layout, you can use a ViewSwitcher to switch between two views, one for the login, and another one for the info you want to show when the user is logged.