gpt4 book ai didi

java - 当我一直按后退按钮登录时,字段显示先前插入的用户数据

转载 作者:行者123 更新时间:2023-12-01 17:50:58 25 4
gpt4 key购买 nike

我正在 Java 上制作一个 Android 应用程序。这个应用程序的问题是,当我在主 fragment 中并按后退按钮时,我希望它返回登录(这种情况发生),但用户名和密码字段显示来自最后一个用户的数据,我想要这些字段显示为空,就像应用程序重新启动一样。

这是我的登录类中的登录用户函数。

public void loginUser() {

String url = MeuSingleton.servidor + "/login";

loginemail = email.getText().toString().trim();
loginpass = pass.getText().toString().trim();

StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
if (response.length() == 9){
Toast.makeText(Login.this, "Login efectuado com sucesso!", Toast.LENGTH_SHORT).show();
Intent i = new Intent(Login.this, MainActivity.class);
startActivity(i);
} else {
mostrarErro(getString(R.string.tituloerro), getString(R.string.login_errado));
}
} catch (Exception e) {
mostrarErro(getString(R.string.tituloerro), getString(R.string.backenderro));
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mostrarErro(getString(R.string.tituloerro), getString(R.string.interneterro));
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("email", loginemail);
map.put("password", loginpass);
return map;
}
};
MeuSingleton.getInstance(this).addToRequestQueue(stringRequest);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(350, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}

这是我的主要 Activity 类(class):

public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener{

MainFragment mainFragment;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
DrawerLayout drawer;
NavigationView navigationView;
ActionBarDrawerToggle toggle;
Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView = findViewById(R.id.nested);
navigationView.setNavigationItemSelectedListener(this);

drawer = findViewById(R.id.drawer);
toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.open,R.string.close);
drawer.addDrawerListener(toggle);
toggle.setDrawerIndicatorEnabled(true);
toggle.syncState();

mainFragment = new MainFragment();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer,mainFragment);
fragmentTransaction.commit();// add the fragment

}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
closeDrawer();
if(menuItem.getItemId() == R.id.submeteralerta){
loadFragment(new InsAlertaFragment());
}
if(menuItem.getItemId() == R.id.veralertas){
loadFragment(new SecondFragment());
}
if(menuItem.getItemId() == R.id.logout){
finish();
}
if(menuItem.getItemId() == R.id.sobremim){
loadFragment(new SobreMimFragment());
}
if(menuItem.getItemId() == R.id.ajuda){
loadFragment(new AjudaFragment());
}
return true;
}

private void loadFragment(Fragment secondFragment) {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer,secondFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}

private void closeDrawer() {
drawer.closeDrawer(GravityCompat.START);
}
}

最后是我的主要 fragment ,我想按回键并转到登录:

public class MainFragment extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main,container,false);
return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}


}

感谢您的帮助!

最佳答案

如果您希望返回时具有空文本字段的登录 Activity ,您可以在启动 MainActivity 之前清除文本字段。例如:

Toast.makeText(Login.this, "Login efectuado com sucesso!", Toast.LENGTH_SHORT).show();
email.setText("");
pass.setText("");
Intent i = new Intent(Login.this, MainActivity.class);
startActivity(i);

关于java - 当我一直按后退按钮登录时,字段显示先前插入的用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60795547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com