Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inflate a layout dialog on another activity?

I am currently on SecondActivity.class . On my code, when I backpress, I want the dialog to appear on my MainActivity.class .

This is my code on inflating the dialog layout.

This appears on SecondActivity.class instead of my main activity.

 View dialog = LayoutInflater.from(this).inflate(R.layout.dialog_fmcg_popup, null);
                                    TextView tvfmcg2 = dialog.findViewById(R.id.tv_fmcg2);
                                    tvfmcg2.setText(message);
                                    swipeDismissDialog = new SwipeDismissDialog.Builder(this)
                                            .setView(dialog)
                                            .setOnSwipeDismissListener(new OnSwipeDismissListener() {
                                                @Override
                                                public void onSwipeDismiss(View view, SwipeDismissDirection direction) {
                                                    Preferences.setString(Prefkey.last_qualified_fmcg_voucher_on_remove, message);
                                                }
                                            })
                                            .setFlingVelocity(0)
                                            .setOverlayColor(0)
                                            .build()
                                            .show();
like image 526
lolo mo Avatar asked Nov 26 '25 04:11

lolo mo


1 Answers

Well what you can use is ActivityForResult() method.

You need to start the activity a little bit different of what you are doing.

startActivityForResult(new Intent(this, SecondActivity.class), 80);

Then in your SecondActivity.class you override the onBackPressed() method as follows

 @Override
    public void onBackPressed() {
        setResult(Activity.RESULT_OK,new Intent());
        finish();
    }

And in your FirstActivity.class you have to override the onActivityResult() method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 80) {
        if(resultCode == Activity.RESULT_OK){
            View dialog = LayoutInflater.from(this).inflate(R.layout.dialog_fmcg_popup, null);
        TextView tvfmcg2 = dialog.findViewById(R.id.tv_fmcg2);
        tvfmcg2.setText(message);
        swipeDismissDialog = new SwipeDismissDialog.Builder(this)
            .setView(dialog)
            .setOnSwipeDismissListener(new OnSwipeDismissListener()
            {
                @Override
                public void onSwipeDismiss(View view, SwipeDismissDirection direction)
                {
                    Preferences.setString(Prefkey.last_qualified_fmcg_voucher_on_remove, message);
                }
            })
            .setFlingVelocity(0)
            .setOverlayColor(0)
            .build()
            .show();
        }
    }
}
like image 167
Skizo-ozᴉʞS Avatar answered Nov 27 '25 16:11

Skizo-ozᴉʞS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!