安装该应用程序后,我只会显示一次警报对话框。在警报对话框中,用户必须选择存储在变量中的国家/地区,然后应用程序才能根据所选国家/地区加载数据。我希望它等待警报对话框处理完毕后再继续...下面是我的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isFirstTime();
initialize();
}
private void isFirstTime() {
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (ranBefore == false) {
String[] countries = getResources().getStringArray(R.array.countries);
final String[] countriesCode = getResources().getStringArray(R.array.countries_code);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
mBuilder.setTitle("Choose country");
mBuilder.setSingleChoiceItems(countries, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String selectedCountry = countriesCode[i];
int selectedCountryIndex = i;
SharedPreferences sharedPref = getSharedPreferences("country", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("selectedCountry", selectedCountry);
editor.putInt("selectedCountryIndex", selectedCountryIndex);
editor.apply();
dialogInterface.dismiss();
}
});
mBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog mDialog = mBuilder.create();
mDialog.show();
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.apply();
}
}
private void initialize(){
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
NavigationDrawer navigationDrawer_fragment =
(NavigationDrawer) getSupportFragmentManager().findFragmentById(R.id.mainfragmentdrawer);
navigationDrawer_fragment.setup((DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.addTab(tabLayout.newTab().setText("Latest"));
tabLayout.addTab(tabLayout.newTab().setText("Categories"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
答案 0 :(得分:0)
您可以更改
private void isFirstTime() {
进入
private bool isFirstTime() {
并根据用户对对话框的响应添加一个返回值,然后从该函数的末尾返回该值。
然后嵌套在其中初始化:
if (isFirstTime()) {
initialize();
}
没有等待吗?嗯,是哪个对话框?
答案 1 :(得分:0)
public static final String COUNTRYSETTINGS = "countrySettings";
private static final String SHOWCNTRYDIALOG = "countryDialog";
Boolean showCountryDialog = false;
//keep a boolean which you set from the sharedpref
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences pref = getSharedPreferences(COUNTRYSETTINGS, 0);
if(!pref.contains(SHOWCNTRYDIALOG)){
pref.edit().putBoolean(SHOWCNTRYDIALOG, false).apply();
showCountryDialog = true;
}else{
showCountryDialog = pref.getBoolean(SHOWCNTRYDIALOG, true);
}
if(showCountryDialog){
createCountryDialog();
}
}
public void createCountryDialog(){
//Handle your setting country related stuff
if(postive_option)
checkCountryAndLoadDataAcc();
}
在共享首选项中创建一个变量,该变量仅在首次安装应用程序时才显示对话框(showCountryDialog),并通过其调用对话框函数可在其中检查位置并调用负载从那里进一步发挥作用。