Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

345
Views
Cerrar un fragmento al hacer clic en el botón que está dentro de ese fragmento

Tengo un fragmento con dos botones: confirmar y cancelar. Un botón, el botón de confirmación, toma la entrada del usuario de EditText y la convierte en una solicitud de USSD. Ahora me gustaría que el botón cancelar cierre ese fragmento como el botón negativo en un cuadro de diálogo de alerta.

He leído muchas preguntas un poco similares a esta, pero ninguna de ellas parece satisfacer mi necesidad.

 public void onClick(View v) { switch (v.getId()) { case R.id.confirmButton: if (inputField.getText().toString().trim().length() == 16) { load(); inputField.requestFocus(); } else if (inputField.getText().toString().trim().length() < 16) { Toast.makeText(getActivity(), R.string.toast_number_not_valid, Toast.LENGTH_SHORT).show(); } break; case R.id.cancelButton: dismiss(); break; } } private void load() { // bla - bla - bla } public void dismiss(){ //dear fragment, I don't need you right now, just dismiss }

Aquí necesito el método de despedir().

Cualquier ayuda será apreciada.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Solo llama:

 getActivity().onBackPressed();
about 4 years ago · Santiago Trujillo Report

0

La solución de oskarko funciona pero te devuelve a la actividad principal.

Si desea que su fragmento se cierre desde el interior de su Fragmento y no desea abandonar la Actividad actual, intente este enfoque:

 getFragmentManager().beginTransaction().remove(ActivityFragment.this).commit();

Donde "ActivityFragment" es el nombre de su Fragmento.

Considere esto : si agrega su FragmentManager agrega el Fragmento a backStack ( fragmentTransaction.addToBackStack(null); ), no debe usar este enfoque, en su lugar use:

 getFragmentManager().popBackStack();

La razón de esto es que si lo cierra con el primer método, su Fragmento todavía está en el backStack y tiene que hacer clic en el Botón Anterior dos veces para llegar a la Actividad anterior.

about 4 years ago · Santiago Trujillo Report

0

Kotlin:

En actividad:

 val fragment = SettingFragment.newInstance() // always create a new transaction to avoid crash val mTransaction = fragmentManager.beginTransaction() mTransaction.add(R.id.settingDrawer, fragment) mTransaction.commit()

En fragmento:

 val manager = requireActivity().supportFragmentManager manager.beginTransaction().remove(this).commit()
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!