gpt4 book ai didi

java - 在 Android Fragment 中实现 Back Pressed

转载 作者:行者123 更新时间:2023-12-01 19:56:35 25 4
gpt4 key购买 nike

我陷入了困境,需要一些帮助。这里有很多关于这个主题的文章,但没有一篇回答我的问题。我想在 fragments 中实现 onBackPressed()并显示dialog显示是否退出应用程序的框。任何帮助将不胜感激。

LoginFragment.java

    public class LoginFragment extends Fragment {
public static final String TAG = LoginFragment.class.getSimpleName();

private EditText mEtEmail;
private EditText mEtPassword;
private Button mBtLogin;
private TextView mTvRegister;
private TextView mTvForgotPassword;
private TextInputLayout mTiEmail;
private TextInputLayout mTiPassword;
private ProgressBar mProgressBar;

private CompositeSubscription mSubscriptions;
private SharedPreferences mSharedPreferences;

@NonNull
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_login,container,false);
mSubscriptions = new CompositeSubscription();
initViews(view);
initSharedPreferences();
return view;
}
private void initViews(View v) {

mEtEmail = v.findViewById(R.id.et_email);
mEtPassword = v.findViewById(R.id.et_password);
mBtLogin = v.findViewById(R.id.btn_login);
mTiEmail = v.findViewById(R.id.ti_email);
mTiPassword = v.findViewById(R.id.ti_password);
mProgressBar = v.findViewById(R.id.progress);
mTvRegister = v.findViewById(R.id.tv_register);
mTvForgotPassword = v.findViewById(R.id.tv_forgot_password);

mBtLogin.setOnClickListener(view -> login());
mTvRegister.setOnClickListener(view -> goToRegister());
mTvForgotPassword.setOnClickListener(view -> showDialog());
}

private void initSharedPreferences() {

mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
}

private void login() {

setError();

String email = mEtEmail.getText().toString();
String password = mEtPassword.getText().toString();

int err = 0;

if (!validateEmail(email)) {

err++;
mTiEmail.setError("Email should be valid !");
}

if (!validateFields(password)) {

err++;
mTiPassword.setError("Password should not be empty !");
}

if (err == 0) {

loginProcess(email,password);
mProgressBar.setVisibility(View.VISIBLE);

} else {

showSnackBarMessage("Enter Valid Details !");
}
}

private void setError() {

mTiEmail.setError(null);
mTiPassword.setError(null);
}

private void loginProcess(String email, String password) {

mSubscriptions.add(NetworkUtil.getRetrofit(email, password).login()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(this::handleResponse,this::handleError));
}

private void handleResponse(Response response) {

mProgressBar.setVisibility(View.GONE);

SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString(Constants.TOKEN,response.getToken());
editor.putString(Constants.EMAIL,response.getMessage());
editor.apply();

mEtEmail.setText(null);
mEtPassword.setText(null);

Intent intent = new Intent(getActivity(), HomeActivity.class);
startActivity(intent);

}

private void handleError(Throwable error) {

mProgressBar.setVisibility(View.GONE);

if (error instanceof HttpException) {

Gson gson = new GsonBuilder().create();

try {

String errorBody = ((HttpException) error).response().errorBody().string();
Response response = gson.fromJson(errorBody,Response.class);
showSnackBarMessage(response.getMessage());

} catch (IOException e) {
e.printStackTrace();
}
} else {

showSnackBarMessage("No Internet Connection!");
}
}

private void showSnackBarMessage(String message) {

if (getView() != null) {

Snackbar.make(getView(),message,Snackbar.LENGTH_SHORT).show();
}
}

private void goToRegister(){

FragmentTransaction ft = getFragmentManager().beginTransaction();
RegisterFragment fragment = new RegisterFragment();
ft.replace(R.id.fragmentFrame,fragment,RegisterFragment.TAG);
ft.addToBackStack(null).commit();
}

private void showDialog(){

ResetPasswordDialog fragment = new ResetPasswordDialog();

fragment.show(getFragmentManager(), ResetPasswordDialog.TAG);
}
@Override
public void onDestroy() {
super.onDestroy();
mSubscriptions.unsubscribe();
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity implements ResetPasswordDialog.Listener {

public static final String TAG = MainActivity.class.getSimpleName();
private LoginFragment mLoginFragment;
private ResetPasswordDialog mResetPasswordDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
loadFragment();
}
}
private void loadFragment() {

if (mLoginFragment == null) {

mLoginFragment = new LoginFragment();
}
getFragmentManager().beginTransaction().replace(R.id.fragmentFrame, mLoginFragment, LoginFragment.TAG).commit();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);

String data = intent.getData().getLastPathSegment();
Log.d(TAG, "onNewIntent: " + data);

mResetPasswordDialog = (ResetPasswordDialog) getFragmentManager().findFragmentByTag(ResetPasswordDialog.TAG);

if (mResetPasswordDialog != null)
mResetPasswordDialog.setToken(data);
}

@Override
public void onPasswordReset(String message) {

showSnackBarMessage(message);
}

private void showSnackBarMessage(String message) {

Snackbar.make(findViewById(R.id.activity_main), message, Snackbar.LENGTH_SHORT).show();

}
}

在我的Login Fragment ,我想显示一个对话框“是否要退出应用程序”。上Yes它会消除当前 fragment 并结束 Activity ,否则它将保持 Activity 状态。请帮忙!

最佳答案

你甚至可以尝试这种方式

MainActivity.java

  @Override
public void onBackPressed() {

if (getFragmentManager() != null && getFragmentManager().getBackStackEntryCount() >= 1) {

String fragmentTag = getFragmentManager().findFragmentById(R.id.frame_container).getTag();

if(fragmentTag.equals(LoginFragment.getTag())){
// show Dialog code
}else{
super.onBackPressed();
}

} else {
super.onBackPressed();
}
}

在您的主 Activity 中添加此代码,以便当添加登录 fragment 并单击 backpress 时,首先如果该 fragment 被添加到 fragment 事务中,那么首先它会找到该 fragment 并检查其标签是否等于登录 fragment 标签。然后,如果两个标记都匹配,则您可以显示退出警报对话框。

关于java - 在 Android Fragment 中实现 Back Pressed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59036405/

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