1

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

Community
  • 1
  • 1
user1977132
  • 487
  • 2
  • 18
  • what is rescanDialogBox? Could you share the code for it? – jayeshsolanki93 Jul 24 '16 at 15:23
  • @jayeshsolanki93 I've added the code for the rescanDialogBox, thank you for any ideas you have. – user1977132 Jul 26 '16 at 09:33
  • Ok, rescanDialogBox looks fine. – jayeshsolanki93 Jul 26 '16 at 10:15
  • 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? – jayeshsolanki93 Jul 26 '16 at 10:18
  • @jayeshsolanki93 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()` – user1977132 Jul 26 '16 at 14:10

2 Answers2

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!

  • 1
    The State fragment itself doesn't have any UI so doesn't get added to a container (onCreateView returns null - see https://developer.android.com/reference/android/app/FragmentTransaction.html ). Thanks for the suggestion though. – user1977132 Jul 26 '16 at 09:17
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

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
alaa
  • 1
  • 1
  • 1