从导航抽屉注销
Logout from Navigation Drawer
我想从我的应用程序导航抽屉中创建一个注销警报对话框。导航抽屉中的所有其他项目都是片段,我希望这个项目只显示一个警告对话框
Navigation Drawer
我之前在我的 MainActivity 中有一个按钮,它使用点击侦听器和 firebase.getInstance.signOut() 将用户带回登录 activity:
点击侦听器上的上一个按钮
//on click listener for logout
/*btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}
});*/
经过一些重新设计后,我创建了一个利用片段的导航抽屉,我希望当用户选择注销时,系统会提示一个警告对话框,询问他们是否确定,然后让按钮按照我之前的方式操作,并且将用户发送回登录 activity.
我认为不需要创建另一个片段来注销,但是如果我错了请纠正我。
Mainactivity.java
public class MainActivity extends AppCompatActivity implements NavigationView.
OnNavigationItemSelectedListener,MainFragment.onFragmentBtnSelected{
public TextView verifyMsg;
public Button btnLogout, verifyEmailBtn;
FirebaseAuth fauth;
public DrawerLayout drawerLayout;
public ActionBarDrawerToggle actionBarDrawerToggle;
public NavigationView navigationView;
AlertDialog.Builder reset_alert;
LayoutInflater inflater;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//xml map to java
//btnLogout = findViewById(R.id.btnLogout);
verifyMsg = findViewById(R.id.verifyEmailMsg);
verifyEmailBtn = findViewById(R.id.btnVerifyEmail);
fauth = FirebaseAuth.getInstance();
reset_alert = new AlertDialog.Builder(this);
inflater =this.getLayoutInflater();
//new stuff
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer);
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
actionBarDrawerToggle = new
ActionBarDrawerToggle(this, drawerLayout,
toolbar, R.string.nav_open, R.string.nav_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
actionBarDrawerToggle.syncState();
//fragment manager - creates instance of a fragment
//load default fragment
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container_fragment,new MainFragment());
fragmentTransaction.commit();
//if the user email is not verified still show verify msg
if(!fauth.getCurrentUser().isEmailVerified()){
verifyEmailBtn.setVisibility(View.VISIBLE);
verifyMsg.setVisibility(View.VISIBLE);
}
//on click listener for verify email button
verifyEmailBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//send verification email
fauth.getCurrentUser().sendEmailVerification()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this,"Verification Sent",Toast.LENGTH_SHORT).show();
verifyEmailBtn.setVisibility(View.GONE);
verifyMsg.setVisibility(View.GONE);
}
});
}
});
}
//able to select the items in nav drawer
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//auto close drawer
drawerLayout.closeDrawer(GravityCompat.START);
//Fragment fragment;
int mId = item.getItemId();
}
if (mId == R.id.homeItem){
//load default fragment
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_fragment,new MainFragment());
fragmentTransaction.commit();
}
if (mId == R.id.profileSettings){
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_fragment,new ProfileFragment());
fragmentTransaction.commit();
}
if (mId == R.id.logoutItem){
}
return true;
}
@Override
public void onBtnSelected() {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_fragment,new ProfileFragment());
fragmentTransaction.commit();
} }
MainFragment.java
public class MainFragment extends Fragment {
private onFragmentBtnSelected listener;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_main,container,false);
Button clickme = view.findViewById(R.id.clickHF);
clickme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onBtnSelected();
}
});
return view;
}
@Override
public void onAttach(@NonNull Context context){
super.onAttach(context);
if(context instanceof onFragmentBtnSelected ){
listener =(onFragmentBtnSelected) context;
}
else {
throw new ClassCastException(context.toString() + " must implement listiner");
}
}
public interface onFragmentBtnSelected{
public void onBtnSelected();
}
}
nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<item android:title="General">
<menu>
<item
android:id="@+id/homeItem"
android:icon="@drawable/ic_baseline_home_24"
android:title="@string/Home" />
</menu>
</item>
<item android:title="Account Settings">
<menu>
<item
android:id="@+id/profileSettings"
android:icon="@drawable/ic_baseline_account_circle_24"
android:title="Profile" />
</menu>
</item>
<item android:title="App Features">
<menu>
<item
android:id="@+id/logoutItem"
android:icon="@drawable/ic_baseline_power_settings_new_24"
android:title="@string/logout" />
</menu>
</item></menu>
非常感谢任何帮助,在导航抽屉中最好地利用片段
Mainactivity.java:
onNavigationItemSelected 方法里面
没有警报对话框
if (mId == R.id.logoutItem){
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}
与警报对话框一起使用
if (mId == R.id.logoutItem){
MaterialAlertDialogBuilder(this)
.setTitle("Title")
.setMessage("Message")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Ok") { _, _ ->
FirebaseAuth.getInstance().signOut();
startActivity(new
Intent(getApplicationContext(),LoginActivity.class));
finish();
}
.setNegativeButton("Cancel") { _, _ ->
Toast.makeText(this, "clicked Cancel", Toast.LENGTH_LONG).show()
}
.show()
}
我想从我的应用程序导航抽屉中创建一个注销警报对话框。导航抽屉中的所有其他项目都是片段,我希望这个项目只显示一个警告对话框
Navigation Drawer
我之前在我的 MainActivity 中有一个按钮,它使用点击侦听器和 firebase.getInstance.signOut() 将用户带回登录 activity:
点击侦听器上的上一个按钮
//on click listener for logout
/*btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}
});*/
经过一些重新设计后,我创建了一个利用片段的导航抽屉,我希望当用户选择注销时,系统会提示一个警告对话框,询问他们是否确定,然后让按钮按照我之前的方式操作,并且将用户发送回登录 activity.
我认为不需要创建另一个片段来注销,但是如果我错了请纠正我。
Mainactivity.java
public class MainActivity extends AppCompatActivity implements NavigationView.
OnNavigationItemSelectedListener,MainFragment.onFragmentBtnSelected{
public TextView verifyMsg;
public Button btnLogout, verifyEmailBtn;
FirebaseAuth fauth;
public DrawerLayout drawerLayout;
public ActionBarDrawerToggle actionBarDrawerToggle;
public NavigationView navigationView;
AlertDialog.Builder reset_alert;
LayoutInflater inflater;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//xml map to java
//btnLogout = findViewById(R.id.btnLogout);
verifyMsg = findViewById(R.id.verifyEmailMsg);
verifyEmailBtn = findViewById(R.id.btnVerifyEmail);
fauth = FirebaseAuth.getInstance();
reset_alert = new AlertDialog.Builder(this);
inflater =this.getLayoutInflater();
//new stuff
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer);
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
actionBarDrawerToggle = new
ActionBarDrawerToggle(this, drawerLayout,
toolbar, R.string.nav_open, R.string.nav_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
actionBarDrawerToggle.syncState();
//fragment manager - creates instance of a fragment
//load default fragment
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container_fragment,new MainFragment());
fragmentTransaction.commit();
//if the user email is not verified still show verify msg
if(!fauth.getCurrentUser().isEmailVerified()){
verifyEmailBtn.setVisibility(View.VISIBLE);
verifyMsg.setVisibility(View.VISIBLE);
}
//on click listener for verify email button
verifyEmailBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//send verification email
fauth.getCurrentUser().sendEmailVerification()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this,"Verification Sent",Toast.LENGTH_SHORT).show();
verifyEmailBtn.setVisibility(View.GONE);
verifyMsg.setVisibility(View.GONE);
}
});
}
});
}
//able to select the items in nav drawer
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//auto close drawer
drawerLayout.closeDrawer(GravityCompat.START);
//Fragment fragment;
int mId = item.getItemId();
}
if (mId == R.id.homeItem){
//load default fragment
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_fragment,new MainFragment());
fragmentTransaction.commit();
}
if (mId == R.id.profileSettings){
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_fragment,new ProfileFragment());
fragmentTransaction.commit();
}
if (mId == R.id.logoutItem){
}
return true;
}
@Override
public void onBtnSelected() {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_fragment,new ProfileFragment());
fragmentTransaction.commit();
} }
MainFragment.java
public class MainFragment extends Fragment {
private onFragmentBtnSelected listener;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_main,container,false);
Button clickme = view.findViewById(R.id.clickHF);
clickme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onBtnSelected();
}
});
return view;
}
@Override
public void onAttach(@NonNull Context context){
super.onAttach(context);
if(context instanceof onFragmentBtnSelected ){
listener =(onFragmentBtnSelected) context;
}
else {
throw new ClassCastException(context.toString() + " must implement listiner");
}
}
public interface onFragmentBtnSelected{
public void onBtnSelected();
}
}
nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<item android:title="General">
<menu>
<item
android:id="@+id/homeItem"
android:icon="@drawable/ic_baseline_home_24"
android:title="@string/Home" />
</menu>
</item>
<item android:title="Account Settings">
<menu>
<item
android:id="@+id/profileSettings"
android:icon="@drawable/ic_baseline_account_circle_24"
android:title="Profile" />
</menu>
</item>
<item android:title="App Features">
<menu>
<item
android:id="@+id/logoutItem"
android:icon="@drawable/ic_baseline_power_settings_new_24"
android:title="@string/logout" />
</menu>
</item></menu>
非常感谢任何帮助,在导航抽屉中最好地利用片段
Mainactivity.java:
onNavigationItemSelected 方法里面 没有警报对话框
if (mId == R.id.logoutItem){
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}
与警报对话框一起使用
if (mId == R.id.logoutItem){
MaterialAlertDialogBuilder(this)
.setTitle("Title")
.setMessage("Message")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Ok") { _, _ ->
FirebaseAuth.getInstance().signOut();
startActivity(new
Intent(getApplicationContext(),LoginActivity.class));
finish();
}
.setNegativeButton("Cancel") { _, _ ->
Toast.makeText(this, "clicked Cancel", Toast.LENGTH_LONG).show()
}
.show()
}