Calling SupportFragmentManager from PauseHandler implementation
Asked Answered
C

2

1

I am trying to implement the PauseHandler described here:

https://mcmap.net/q/159989/-how-to-handle-handler-messages-when-activity-fragment-is-paused

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://mcmap.net/q/112346/-cannot-resolve-method-39-getsupportfragmentmanager-39-inside-fragment

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

Cardin answered 24/7, 2016 at 15:2 Comment(5)
what is rescanDialogBox? Could you share the code for it?Heptangular
@Heptangular I've added the code for the rescanDialogBox, thank you for any ideas you have.Cardin
Ok, rescanDialogBox looks fine.Heptangular
Why are you creating a new activity variable inside processMessage when you already have it as a member field of PauseHandlerImplementaion class? Also have you tried using getActivity() instead?Heptangular
@Heptangular I was just copying the code from the first answer I linked to, they seemingly duplicate the activity variable. I don't know why. I've tried replacing it with getActivity(), but then the error changes to Cannot resolve method getActivity()Cardin
C
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!

Corker answered 24/7, 2016 at 21:48 Comment(1)
The State fragment itself doesn't have any UI so doesn't get added to a container (onCreateView returns null - see developer.android.com/reference/android/app/… ). Thanks for the suggestion though.Cardin
M
0

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

Mandy answered 25/9, 2016 at 20:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.