javaandroiddialogfragmentandroid-wake-lock

Prevent DialogFragment from sending the app into energy save mode


I'm creating an App for an Android Wearable Device.

When showing a custom layout as a dialogue fragment, the app will get minimized (getting replaced by the watch face) after about 30 seconds. I know that this is supposed to be the default behavior of apps out of touch with their users, but in my case, the app needs to stay visible even if not touched four minutes.

The Activities calling it did get these instructions inside their onCreate implementation.

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

that works like a charm and in the way I want it. However, the DialogFragment that serves as an error notification (and may need to get observed without touching it for some time) does not obey this setting.

I tried to get the flag inside the DialogFragment too by placing it inside the onViewCreated calls, but it does not have the getWindow Method. While the next code segment is valid, is does not work either.

getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

The way the dialog fragment is called looks like this. That's the code to call it from the FragmentActivity needing to show it. As you might notice, there is a field "activity", which is because the whole call is done from a static class outside the activity. I want to call the same DialogFragment from multiple activities, with only the text and the title being different.

   public static void showDialogCuston(String title, String message, FragmentActivity activity){

        ErrorDialogFragment edf = ErrorDialogFragment.newInstance(title,message);

        FragmentManager fm = activity.getSupportFragmentManager();
        edf.show(fm, "TAG");
    }

And that's what the DialogFragment does look like inside. I kicked out all TextView text assignments because I doubt that they could offer any kind of information about that request, and just making the code fragment less readable.

public class ErrorDialogFragment extends DialogFragment {

    //some private text views
    public ErrorDialogFragment(){

    }

    public static ErrorDialogFragment newInstance(String title, String text){
        ErrorDialogFragment edf = new ErrorDialogFragment();
        Bundle args = new Bundle();
        args.putString("title",title);
        args.putString("text",text);
        edf.setArguments(args);
        return edf;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_error, container, false);
        // R.layout.fragment_error is the layout that serves as my custom dialog
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //assigning layout elements to private fields
        //assigning stuff from the bundle inside some textViews

        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                //playing a sound from SoundPool and doing haptic feedback on button press
            }
        });
    }
}

So, all I want is that the app won't disappear by itself when one is staring too long on the DialogFragment without touching it.


Solution

  • In your case, I would like to suggest keeping the screen-on flag in the dialog layout file so that when the dialog view is visible, it keeps the screen on. Check the developer's documentation for more information. However, I am adding the code from the documentation for convenience.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:keepScreenOn="true">
        ...
    </RelativeLayout>
    

    Set the android:keepScreenOn to true in the root element of your dialog layout. And I hope you also have the following permission in your AndroidManifest.xml file.

    <uses-permission android:name="android.permission.WAKE_LOCK" />