Keep dialog/activity always on the top

孤街浪徒 提交于 2019-12-07 12:12:26

问题


How to keep a dialog/activity on the top of other activities, no matter if user switch between activities,it should be alive all the time.


回答1:


You can use Relative layout as a parent, by using Relative Layout, you can overlap the other layout. So, you have to use to two child layout of relative layout. In the one child you will have popup, and in another layout you have to keep changing your layout..

If you want this across multiple activities. You must create a separate layout and include that in all activities, and create an interface to handle the button events in the popup.

or

You can create a base activity, having above mentioned layout, and extends that activity in all other activities where you want this layout.

Regards, Yuvi




回答2:


Personnaly, I will do something like that :

1) Create a class which extends from DialogFragment :

    public class MyDialogFragment extends DialogFragment{
        public static final int DIALOG_TYPE1 = 1;

        public static MyDialogFragment newInstance(int dialogType) {
                MainDialogFragment frag = new MainDialogFragment();
                Bundle args = new Bundle();
                args.putInt("type", dialogType);
                frag.setArguments(args);
                return frag;
            }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
                super.onCreateDialog(savedInstanceState);
                int type = getArguments().getInt("type");
                Dialog result = null;
                switch (type) {
                case DIALOG_TYPE1:
                              result = new AlertDialog.Builder(getActivity())
                                 .setTitle("TITLE")
                                 .setMessage("MESSAGE")
                                 .setPositiveButton(android.R.string.ok, null)
                                 .create();
                              break;
                        default:
                              break;
                }
                return result;
         }
}

2) Then in your activities :

DialogFragment dialog = MyDialogFragment.newInstance(MyDialogFragment.DIALOG_TYPE1);
dialog.show(getFragmentManager(), "DIALOG");

3) And you put in a bundle the type of the dialog that the next activity can get it and show it again.



来源:https://stackoverflow.com/questions/15812777/keep-dialog-activity-always-on-the-top

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!