从PauseHandler实现中调用SupportFragmentManager [英] Calling SupportFragmentManager from PauseHandler implementation

查看:152
本文介绍了从PauseHandler实现中调用SupportFragmentManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现此处描述的PauseHandler:

I am trying to implement the PauseHandler described here:

https://stackoverflow.com/a/8122789/1977132

我的活动是一个ActionBarActivity,打开对话框的processMessage方法中的代码给出错误Cannot resolve method 'getSupportFragmentManager'

My activity is an ActionBarActivity, the code in the processMessage method which brings up a dialog gives the error Cannot resolve method 'getSupportFragmentManager'

按照这里的建议: https://stackoverflow.com/a/27425568/1977132

我试图将其更改为getFragmentManager(),但随后出现错误Cannot resolve method 'show(android.app.FragmentManager(), java.lang.String'

I tried to change it to getFragmentManager() but then I get the error Cannot resolve method 'show(android.app.FragmentManager(), java.lang.String'

我在这里粘贴了我认为是相关的代码:

I have pasted what I believe to be the relevant code here:

import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;

public class PlaySession extends ActionBarActivity implements View.OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            final Fragment state = new State();
            final FragmentManager fm = getSupportFragmentManager();
            final FragmentTransaction ft = fm.beginTransaction();
            ft.add(state, State.TAG);
            ft.commit();
        }
    }

    public static class rescanDialogBox extends DialogFragment {
        final static String TAG = "RESCAN";

        public static rescanDialogBox newInstance(int title) {
            rescanDialogBox frag = new rescanDialogBox();
            Bundle args = new Bundle();
            args.putInt("title", title);
            frag.setArguments(args);
            return frag;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            String title = "Rescan?";
            String message = "No Bluetooth LE devices found. Do you want to rescan? If you keep getting this message check the battery of your Bluetooth device.";

            AlertDialog.Builder myDialogBuilder = new AlertDialog.Builder(getActivity());
            myDialogBuilder.setTitle(title);
            myDialogBuilder.setMessage(message);
            myDialogBuilder.setPositiveButton("YES",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        ((PlaySession) getActivity()).yesRescan();
                    }
                });
            myDialogBuilder.setNegativeButton("NO",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        ((PlaySession) getActivity()).noRescan();
                    }
                });

            AlertDialog myDialog = myDialogBuilder.create();
            myDialog.setCanceledOnTouchOutside(false);

            return myDialog;
        }
    }

    public void yesRescan() {
        //rescan
    }

    public void noRescan() {
        //do nothing
    }

    final static class State extends Fragment {
        static final String TAG = "State";
        public PauseHandlerImplementation handler = new PauseHandlerImplementation();

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRetainInstance(true);
        }

        @Override
        public void onResume() {
            super.onResume();
            handler.setActivity(getActivity());
            handler.resume();
        }

        @Override
        public void onPause() {
            super.onPause();
            handler.pause();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            handler.setActivity(null);
        }
    }


    static class PauseHandlerImplementation extends PauseHandler {
        protected Activity activity;

        final void setActivity(Activity activity) {
            this.activity = activity;
        }

        @Override
        final protected void processMessage(String toDo) {
            final Activity activity = this.activity;

            if (activity != null) {
                if(toDo.equals("OFFER_RESCAN")) {
                    //offer rescan
                    DialogFragment newFragment = new rescanDialogBox();
                    newFragment.show(activity.getSupportFragmentManager(), rescanDialogBox.TAG);
                    activity.getSupportFragmentManager().executePendingTransactions();

                } else if (toDo.equals("OFFER_DEVICES")) {
                    //offer devices
                }
            }
        }
    }
}

当然,还有用于扩展Handler的PauseHandler的代码和用于对话框的代码,但是可以正常工作,因此似乎无关紧要.

Of course there is also the code for the PauseHandler which extends Handler and the code for the dialog box but that was/is working ok so doesn't seem relevant.

编辑 添加了重新扫描对话框的代码,在添加PauseHandler之前,此对话框可以正常工作,但是我需要增加对在扫描过程中用户离开应用程序导航的情况的支持,因此需要PauseHandler

EDIT Added code for the rescan dialog box, this dialog box worked fine before I added the PauseHandler but I needed to add support for the case where the user navigates away from the app while the scan is in progress, hence the need for the PauseHandler

推荐答案

我遇到了与您相同的问题,我认为您应该更改

I had the same problem as yours, I think you should change

import android.support.v4.app.DialogFragment

import android.app.DialogFragment;

在您的DialogFragment班上

这篇关于从PauseHandler实现中调用SupportFragmentManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆