我正在开发一个Android应用程序,允许用户使用他们的Google帐户登录,当用户使用他的Google帐户登录时,会出现一个对话框,要求他介绍一些数据(年龄和性别)。
这是我的DialogBox类:
public class DialogBox extends DialogFragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {
private static final String TAG = DialogBox.class.getSimpleName();
private Button okButton;
private Spinner ageSpinner;
private RadioButton radioDialogMale;
private RadioButton radioDialogFemale;
private String selectedAge;
private Communicator communicator;
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log.d(TAG, "onCreateDialog(Bundle) - Ini ");
Log.d(TAG, "onCreateDialog(Bundle) - Fi ");
return super.onCreateDialog(savedInstanceState);
}
public void onAttach(Activity activity) {
super.onAttach(activity);
communicator = (Communicator) activity;
}
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "onCreateView(LayoutInflaterm ViewGroup, Bundle) - Ini ");
View view = inflater.inflate(R.layout.dialog_box_layout, null);
ageSpinner = (Spinner) view.findViewById(R.id.age_spinner_dialog);
ageSpinner.setSelected(false);
ageSpinner.setAdapter(populateAgeSpinner());
ageSpinner.setOnItemSelectedListener(this);
radioDialogMale = (RadioButton) view.findViewById(R.id.radioDialogM);
radioDialogMale.setSelected(false);
radioDialogMale.setOnClickListener(this);
radioDialogFemale = (RadioButton) view.findViewById(R.id.radioDialogF);
radioDialogFemale.setSelected(false);
radioDialogFemale.setOnClickListener(this);
okButton = (Button) view.findViewById(R.id.dialog_ok);
okButton.setOnClickListener(this);
setCancelable(false);
Log.d(TAG, "onCreateView(LayoutInflaterm ViewGroup, Bundle) - Fi ");
return view;
}
@Override
public void onClick(View v) {
Log.d(TAG, "onClick(View) - Ini ");
switch (v.getId()) {
case R.id.radioDialogF:
radioDialogFemale.setSelected(true);
break;
case R.id.radioDialogM:
radioDialogMale.setSelected(true);
break;
case R.id.dialog_ok:
if(validate()) {
String gender = "M";
if(radioDialogFemale.isSelected()) {
gender = "F";
}
communicator.onDialogValues(gender,Integer.parseInt(selectedAge));
}
break;
}
Log.d(TAG, "onClick(View) - Ini ");
}
// To fill the age spinner
public ArrayAdapter<Integer> populateAgeSpinner () {
Log.d(TAG ,"populateAgeSpinner - Ini");
List age = new ArrayList<String>();
age.add("");
for(int i = 18; i <= 100; i++) {
age.add(Integer.toString(i));
}
ArrayAdapter<Integer> spinnerAgeAdapter = new ArrayAdapter<Integer>(getActivity(),R.layout.spinner_item_layout, age);
spinnerAgeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Log.d(TAG ,"populateAgeSpinner - Fi");
return spinnerAgeAdapter;
}
public boolean validate() {
Log.d(TAG ,"validate() - Ini");
boolean result = true;
if(selectedAge.equals(null) || selectedAge.isEmpty()) {
TextView errorText = (TextView)ageSpinner.getSelectedView();
errorText.setError(getResources().getString(R.string.Error_register_age));
result = false;
}
if(!radioDialogFemale.isSelected() && !radioDialogMale.isSelected()) {
radioDialogFemale.setError(getResources().getString(R.string.Error_register_gender));
radioDialogMale.setError(getResources().getString(R.string.Error_register_gender));
result = false;
}
Log.d(TAG ,"validate() - Fi");
return result;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedAge = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public interface Communicator {
void onDialogValues(String gender, int age);
}
}
这里是调用对话框的Activity:
public class LoginActivity extends BaseActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener
,DialogBox.Communicator
{
................
protected void updateActivity(FirebaseUser user) {
Log.d(TAG ,"updateActivity(FirebaseUser) - Ini");
if(user != null) {
ArrayList<String> providers = (ArrayList<String>) user.getProviders();
if(!providers.isEmpty() && providers.get(0).equals("google.com")) {
username = user.getEmail().substring(0,user.getEmail().indexOf('@'));
username = "mary";
String userId = user.getUid();
final boolean[] exist = {false};
DatabaseReference databaseReference = database.getReference();
Query query = databaseReference.child("Users").orderByChild(userId).equalTo(userId);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot userSnapShot : dataSnapshot.getChildren()){
if (!userSnapShot.getKey().equals(null) && !userSnapShot.getKey().isEmpty()) {
exist[0] = true;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
if(!exist[0]) {
// DialogBox dialog = new DialogBox();
showDialog();
}
}
Intent i = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(i);
finish();
}
Log.d(TAG ,"updateActivity(FirebaseUser) - Fi");
}
public void showDialog() {
Log.d(TAG, "showDialog(View) - Ini");
FragmentManager manager = getFragmentManager();
DialogBox dialog = new DialogBox();
dialog.show(manager,"DialogBox");
Log.d(TAG, "showDialog(View) - Ini");
}
@Override
public void onDialogValues(String sexe, int age) {
gender = sexe;
selectedAge = String.valueOf(age);
}
}
显示对话框但执行没有停止,而且行:
Intent i = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(i);
finish();
执行,因此其他活动开始,用户没有时间在对话框中引入请求的数据。
我将非常感谢您解决此问题的任何帮助。
答案 0 :(得分:1)
让您的活动实施DialogInterface.OnDismissListener
,然后在onDismiss
方法内创建下一个活动的意图并启动它。
Intent i = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(i);
finish();
答案 1 :(得分:0)
完成代码将始终运行,因为对话框打开并不会阻止代码继续运行。
因此,您必须在DialogFragment()
方法的updateActivity()
NOT的dismiss方法中运行完成代码:
public void showDialog() {
Log.d(TAG, "showDialog(View) - Ini");
FragmentManager manager = getFragmentManager();
DialogBox dialog = new DialogBox();
dialog.show(manager, "DialogBox");
dialog.onDismiss(dismiss());
Log.d(TAG, "showDialog(View) - Ini");
}
private DialogInterface dismiss() {
Intent i = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(i);
finish();
return null;
}