简体   繁体   中英

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();

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();
        }
    }
}

尝试调用方法而不是在MainActivity onStartonResume中显示对话框

If you want to display this on your MainActivity instead of your SecondActivity you must start the SecondActivity like this:

startActivityForResult(yourIntent, SECOND_ACTIVITY_KEY)

On BackPresses you can set the result like this

Intent intent=Intent()
intent.putExtra("MESSAGE",message)
setResult(2,intent)

Then on MainActivity you can listen for results and call your dialog.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    // Check which request we're responding to
    if (requestCode == SECOND_ACTIVITY_KEY) {
        // Make sure the request was successful
        if (resultCode == Activity.RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM