Currently my code loads a TabWidget with 4 tabs. The first tab points to a disclaimer page which also takes in a username from a edittext box and stores it internally on the phone. I would like to have an if condition which checks to see if the username is stored on the phone and if it is then show the user a different “thank-you” page than the original disclaimer page. I believe my code isn’t working because it’s under the onCreate function and isn’t refreshing…
public class TabWidgetActivity extends TabActivity {
/** 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; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
String username = null;
String pulledUsername = getPreferences(MODE_PRIVATE).getString("username",username);
// ******* MAIN PART I'M HAVING TROUBLE WITH ********************************
if (pulledUsername != null){
intent = new Intent().setClass(this, HomeActivity.class);
}
else {
intent = new Intent().setClass(this, DisclaimerActivity.class);
}
// ^^^^^^^^^^^^^^^ MAIN PART I'M HAVING TROUBLE WITH ^^^^^^^^^^^^^^^^^^^^^^
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("home").setIndicator("Home",
res.getDrawable(R.drawable.ic_tab_home))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ShowMapActivity.class);
spec = tabHost.newTabSpec("map").setIndicator("Map",
res.getDrawable(R.drawable.ic_tab_map))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SurveyActivity.class);
spec = tabHost.newTabSpec("survey").setIndicator("Survey",
res.getDrawable(R.drawable.ic_tab_web))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, AnalysisActivity.class);
spec = tabHost.newTabSpec("analysis").setIndicator("Analysis",
res.getDrawable(R.drawable.ic_tab_analysis))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
Can you be a bit more clear about what “isn’t working”?
Are you setting the username in the preferences within this Activity? If not then you should be using
getSharedPreferences()as preferences are stored on a per activity basis.when writing the username in the Disclaimer Activity
When pulling the username in the other activity: