从PauseHandler实现调用SupportFragmentManager

时间:2016-07-24 15:02:23

标签: java android android-fragments android-support-library android-handler

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

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

我的活动是ActionBarActivity,processMessage方法中的代码会弹出一个对话框,提供错误Cannot resolve method 'getSupportFragmentManager'

遵循此处的建议: https://stackoverflow.com/a/27425568/1977132

我尝试将其更改为getFragmentManager(),但后来我收到错误Cannot resolve method 'show(android.app.FragmentManager(), java.lang.String'

我已粘贴了我认为相关的代码:

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
                }
            }
        }
    }
}

当然还有PauseHandler的代码,它扩展了Handler和对话框的代码,但是/它工作正常,所以看起来并不相关。

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

2 个答案:

答案 0 :(得分:0)

首先,ActionBarActivity现已弃用。您可能应该使用AppCompatActivity来支持以前的Android版本。

其次,您应该使用方法.replace(int ontainerViewId, Fragment fragment)指定容器放置State片段的位置。您的方法应如下所示:

final Fragment state = new State();
final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.your_container_id, state);
ft.commit();

希望它有所帮助!

答案 1 :(得分:0)

我和你的问题一样,我认为你应该改变

import android.support.v4.app.DialogFragment

import android.app.DialogFragment;

在您的DialogFragment班级