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
activity
variable insideprocessMessage
when you already have it as a member field ofPauseHandlerImplementaion
class? Also have you tried using getActivity() instead? – HeptangularCannot resolve method getActivity()
– Cardin