我创建了一个登录布局(从 MainActivity 开始)public class MainActivity extends AppCompatActivity implements View.OnClickListener, BottomNavigationView.OnNavigationItemSelectedListener{ private Button regiBtn; private Button loginBtn; private Button forgotBtn; private EditText loginEmail; private EditText loginPwd; public static FirebaseAuth mAuth; public static FirebaseUser currentUser; @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { Fragment fragment = null; switch (menuItem.getItemId()){ case R.id.navigation_profile: fragment = new ProfileFragment(); break; case R.id.navigation_task: fragment = new TaskFragment(); break; case R.id.navigation_leadingboard: fragment = new LeadingBoardFragment(); break; case R.id.navigation_home: fragment = new HomeFragment(); break; } return loadFragment(fragment); } private boolean loadFragment(Fragment fragment){ if(fragment!=null){ getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment_container, fragment) .commit(); return true; } return false; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mAuth = FirebaseAuth.getInstance(); // check if user is signed in (non-null) currentUser = mAuth.getCurrentUser(); }正如您所看到的,当用户登录时,MainActivity 将其布局更改为底部导航布局,用户可以在其中选择要执行特定操作的部分。我想将当前活动(应该是MainActivity对吧?)布局更改为activity_main(用户执行登录功能的地方),布局确实改变了,但按钮无法工作。布局更改为activity_main(用户登录)->activity_dashboard(带有片段)->main_activity(当用户从片段注销时)我想知道我哪里出了问题。
2 回答
胡说叔叔
TA贡献1804条经验 获得超8个赞
布局确实改变了,但按钮不起作用。
MainActivity 扩展到AppCompatActivity. 您应该使用Intent打开 MainActivity,而不是创建新的setContentView.
logoutBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
MainActivity.mAuth.signOut();
Intent i = new Intent(getActivity(),MainActivity.class);
startActivity(i);
getActivity().finish();
}
});
一只斗牛犬
TA贡献1784条经验 获得超2个赞
在您的“配置文件片段”中,在“注销功能”中尝试使用以下代码:
Intent in = new Intent(getActivity(),MainActivity.class);
startActivity(in);
getActivity().finish();
添加回答
举报
0/150
提交
取消