Keep dialog/activity always on the top

2019-05-13 18:37发布

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.

Look at Show, Hide buttons

2条回答
放荡不羁爱自由
2楼-- · 2019-05-13 19:15

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.

查看更多
神经病院院长
3楼-- · 2019-05-13 19:22

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

查看更多
登录 后发表回答