当前片段在Firebase中更改数据后重定向到上一个活动

时间:2018-02-09 12:10:51

标签: android firebase android-fragments firebase-realtime-database

在我更改Firebase中的数据后,当前片段将重定向到已启动的活动。我需要更改 ManageTeam 片段中按钮的可见性。应用程序的流程是 LoginActivity - > MainActivity - >的 ManageTeam

LoginActivity.java

public class LoginActivity extends AppCompatActivity {


private MaterialEditText etEmail,etPassword;
private Button btnLogin,btnSignUp;
private FirebaseAuth firebaseAuth;
LinearLayout linearLayout;
private String role;
private User student;
private ProgressDialog progressDialog;
DatabaseReference databaseReference;
ValueEventListener valueEventListener;
private static String user_field = "user";
private static String pwd_field = "pwd";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    Paper.init(this);
    progressDialog = new ProgressDialog(LoginActivity.this);
    progressDialog.setTitle("MTCHY");
    progressDialog.setMessage("Login");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    etEmail = (MaterialEditText) findViewById(R.id.etLoginEmail);
    etPassword = (MaterialEditText) findViewById(R.id.etLoginPassword);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnSignUp = (Button) findViewById(R.id.btnLoginSignUp);
    firebaseAuth = FirebaseAuth.getInstance();
    linearLayout = (LinearLayout) findViewById(R.id.loginLayout);

    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String email = etEmail.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password))
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                builder.setMessage("Please enter an email and password").setTitle("Warning").setPositiveButton("OK",null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }

            else
            {

                progressDialog.show();
                firebaseAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {


                        if(task.isSuccessful())
                        {
                             databaseReference = FirebaseDatabase.getInstance().getReference();


                            valueEventListener = databaseReference.child("Users").orderByChild("email").equalTo(email).addValueEventListener(new ValueEventListener() {
                               @Override
                               public void onDataChange(DataSnapshot dataSnapshot) {
                                   Paper.book().write(user_field,etEmail.getText().toString());
                                   Paper.book().write(pwd_field,etPassword.getText().toString());
                                   for(DataSnapshot userDataSnapshot : dataSnapshot.getChildren())
                                   {
                                       student = userDataSnapshot.getValue(User.class);
                                       role = student.getUserRole();
                                       if (role.equals("Player"))
                                       {
                                           progressDialog.dismiss();
                                           startActivity(new Intent(LoginActivity.this,MainActivity.class));
                                           finish();

                                       }

                                       if (role.equals("Captain"))
                                       {
                                           progressDialog.dismiss();
                                           startActivity(new Intent(LoginActivity.this,MainActivity.class));
                                           finish();


                                       }
                                   }

                               }

                               @Override
                               public void onCancelled(DatabaseError databaseError) {
                                   progressDialog.dismiss();
                                   AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                                   builder.setMessage(databaseError.getMessage()).setTitle("Database Error").setPositiveButton("OK",null);
                                   AlertDialog dialog = builder.create();
                                   dialog.show();


                               }
                           });


                        }


                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {

                        progressDialog.dismiss();
                        AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                        builder.setMessage(e.getMessage()).setTitle("Login Error").setPositiveButton("OK",null);
                        AlertDialog dialog = builder.create();
                        dialog.show();

                    }
                });
            }


        }
    });

    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(LoginActivity.this,SignupActivity.class);
            startActivity(intent);

        }
    });


    String papsuser = Paper.book().read(user_field);
    String pappwd = Paper.book().read(pwd_field);
    System.out.println(papsuser);
    System.out.println(pappwd);

    if (papsuser != null && pappwd != null)
    {
        if(!TextUtils.isEmpty(papsuser) && !TextUtils.isEmpty(pappwd))
        {
            autoLogin(papsuser,pappwd);
        }

    }
}


@Override
protected void onDestroy() {
    super.onDestroy();
    databaseReference.removeEventListener(valueEventListener);
}

}

MainActivity.java

public class MainActivity extends AppCompatActivity {

private BottomNavigationView navigation;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AHBottomNavigation bottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_navigation);

 AHBottomNavigationItem item1 = new AHBottomNavigationItem("Home", R.drawable.common_google_signin_btn_icon_dark_focused, R.color.colorBottomNavigationDisable);
    AHBottomNavigationItem item2 = new AHBottomNavigationItem("Manage Team", R.drawable.common_google_signin_btn_icon_dark_focused, R.color.colorAccent);
    AHBottomNavigationItem item3 = new AHBottomNavigationItem("Setting", R.drawable.common_google_signin_btn_icon_dark_focused,R.color.colorPrimary);

    bottomNavigation.addItem(item1);
    bottomNavigation.addItem(item2);
    bottomNavigation.addItem(item3);


    bottomNavigation.setDefaultBackgroundColor(getResources().getColor(R.color.colorBackgroud));
    bottomNavigation.setCurrentItem(0);
    bottomNavigation.setAccentColor(getResources().getColor(R.color.colorPrimary));
    bottomNavigation.setInactiveColor(getResources().getColor(R.color.colorAccent));
    bottomNavigation.setForceTint(true);
    bottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
    AFragment aFragment = new AFragment();
    getSupportFragmentManager().beginTransaction().replace(R.id.content, aFragment).commit();

    bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
        @Override
        public boolean onTabSelected(int position, boolean wasSelected) {


            if(position == 0) {
                AFragment aFragment = new AFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.content, aFragment).commit();
                return true;
            }
            else if(position == 1 )
            {
                ManageTeam manageTeam = new ManageTeam();
                getSupportFragmentManager().beginTransaction().replace(R.id.content,manageTeam).commit();
                return true;
            }
            return false;
        }
    });


}

}

ManageTeam.java

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


    btn = (Button) getView().findViewById(R.id.btnJohn);
    users =  FirebaseDatabase.getInstance().getReference("Users");

    value = users.orderByChild("email").equalTo("viginesjohn2@gmail.com").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            for(DataSnapshot clubDataSnapshot : dataSnapshot.getChildren())
            {
                User user = clubDataSnapshot.getValue(User.class);

                if (user.getHasTeam() == true)
                {
                    btn.setVisibility(View.GONE);
                }

                if (user.getHasTeam() == false)
                {

                    btn.setVisibility(View.VISIBLE);
                }
            }

        }


        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    users.removeEventListener(value);

}

火力地堡 My firebase data

UI Android UI 根据用户界面,活动将在Home上开始。在Firebase中将 hasTeam 字段更改为 true false 之后。 Fragment更新了UI,但它将重定向回Home Activity。

0 个答案:

没有答案