当前我的代码加载了一个带有4个选项卡的TabWidget
。第一个选项卡指向免责声明页面,该页面还从编辑文本框中获取用户名,并将其内部存储在手机上。我想要一个if条件,检查用户名是否存储在手机上,如果是,则向用户显示与原始免责声明页面不同的"感谢"页面。我相信我的代码不起作用,因为它在onCreate函数下,没有刷新。。。
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);
}
}
您能更清楚地了解什么是"不起作用"吗?
您是否在此"活动"中的首选项中设置了用户名?如果没有,那么您应该使用getSharedPreferences()
,因为首选项是在每个活动的基础上存储的。
在免责声明活动中写入用户名时
SharedPreferences settings = getSharedPreferences("com.blah.application", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", username);
// Commit the edit!
editor.apply();
在其他活动中提取用户名时:
pulledUsername = getSharedPreferences("com.blah.application", MODE_PRIVATE).getString("username",null)