0

Lo que yo quiero hacer es que tengo varias clases Java con sus respectivos fragment.xml, además tengo un alert_dialog.xml con su respectivo AlertDialog.java, lo que yo quiero hacer es que dependiendo del fragment el alert_dialog muestre un titulo y mensaje diferente.

CheckInFragment.java

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.check_in, container, false);

    Button check_in_button = view.findViewById(R.id.check_in_button);
    check_in_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alertDialog(v);
        }
    });

    fillFields(view);
    return view;
}

private void alertDialog(View view){
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    LayoutInflater layoutInflater = getLayoutInflater();
    View alertDialog = View.inflate(getContext() , R.layout.alert_dialog, null);
    builder.setView(alertDialog);

    int width = (int)(getResources().getDisplayMetrics().widthPixels * 0.40);
    int height = (int)(getResources().getDisplayMetrics().heightPixels * 0.40);
    alertDialog.setMinimumWidth(width);
    alertDialog.setMinimumHeight(height);

    //Titulo y mensaje del alert_dialog
    String title = getResources().getString(R.string.alert_title_token);
    String message = getResources().getString(R.string.alert_message_token);

    Intent intent = new Intent(getActivity(), AlertDialog.class);
    intent.putExtra("title", title);
    intent.putExtra("message", message);

    builder.show();
}

AlertDialog.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alert_dialog);

    String title = getIntent().getStringExtra("title");
    String message = getIntent().getStringExtra("message");

    TextView alert_title = findViewById(R.id.alert_title);
    TextView alert_message = findViewById(R.id.alert_message);

    alert_title.setText(title);
    alert_message.setText(message);
}

No puedo poner startActivity(intent) en mi checkinFragemnt ya que me mostraría el activity completo del alert_dialog y ya yo lo estoy mostrando con otra acción, yo solo quiero pasar los datos.

En mi CheckInFragment si almacena bien los datos pero el AlertDialog no los recibe

1 respuesta 1

1

Si lo que quieres es realizar o montar una acción diferente según el Fragment en el que estes posicionado, debes usar el evento NavHostFragment. Entonces, tu botón, debería quedar algo como esto:

check_in_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment); // cambia el nav_host_fragment acorde al tuyo

        if (navHostFragment != null) {
            Fragment currentFragment = navHostFragment.getChildFragmentManager().getFragments().get(0);

            if (currentFragment instanceof HomeFragment) {
                Toast.makeText(MainActivity.this, "Fragment 1", Toast.LENGTH_SHORT).show();

            }//luego sigues comparando
        }
        }
    });

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.