Calling SupportFragmentManager from PauseHandler implementation

对着背影说爱祢 提交于 2019-12-12 03:16:45

问题


I am trying to implement the PauseHandler described here:

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

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

Following the suggestion here: https://stackoverflow.com/a/27425568/1977132

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

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.

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


回答1:


First of all, ActionBarActivityis now deprecated. You probably should use AppCompatActivity to be able to support previous Android version.

Second, you should specify the container where to put your State fragment, with the method .replace(int ontainerViewId, Fragment fragment). Your method should look like this:

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

Hope it helps!




回答2:


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

import android.support.v4.app.DialogFragment

to

import android.app.DialogFragment;

in your DialogFragment class



来源:https://stackoverflow.com/questions/38553430/calling-supportfragmentmanager-from-pausehandler-implementation

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