-1

The 'setRequestedOrientation' method restarts the activity normally on any version of the android higher than Kitkat.

But in Kitkat, even using if, the activity continues to restart.

    int orientation = getResources().getConfiguration().orientation;

    // Doesn't work
    if (orientation != ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    }

    // Doesn't work
    if (orientation != Configuration.ORIENTATION_LANDSCAPE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    }

EDIT 1 - StackTrace:

https://gist.github.com/sshnakamoto/11ef6179a561054e54ec4d41a03238f0

Sorry, my log is too long to post here. I've created a gist. But essentially you will see a loop between onCreate() and onStart() methods.

EDIT 2 - ActivityCode:

public class TestActivity extends AppCompatActivity {

    private static final String TAG = "TimerActivityCLONE";

    private TextView textView;
    private ConstraintLayout parentView;
    private boolean isColorChanged;

    private int textColor;
    private int parentColor;

    private Handler handler;
    private Runnable runnable;
    private Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate: started ");
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: super called ");
        setContentView(R.layout.activity_timer);
        Log.d(TAG, "onCreate: setContentView called ");

        /* Find on layout*/
        parentView = findViewById(R.id.parent);
        textView = findViewById(R.id.textView);

        textColor = Color.WHITE;
        parentColor = Color.BLACK;

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

    }

    private void initTimer() {

        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                changeColors();
            }
        };

        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                handler.post(runnable);
            }
        };

        timer = new Timer();
        timer.schedule(timerTask, 0, 1000);
    }

    private void changeColors() {

        Log.d(TAG, "changeColors: size " + textView.getTextSize() / getResources().getDisplayMetrics().scaledDensity);

        if (isColorChanged){
            textView.setTextColor(parentColor);
            parentView.setBackgroundColor(textColor);
            isColorChanged = false;
        } else {
            textView.setTextColor(textColor);
            parentView.setBackgroundColor(parentColor);
            isColorChanged = true;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: ");
        /* Start to show */
        initTimer();
    }

    @Override
    protected void onStop() {
        super.onStop();
        killTaskAndFinish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        killTaskAndFinish();
    }

    private void killTaskAndFinish() {

        /* Kill background Thread */
        timer.cancel();
        timer.purge();
        handler.removeCallbacks(runnable);

        /* Restore user screen orientation */
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
    }
}

I've found the bug, it occurs when calling method killTaskAndFinish() inside onStop() due ResquestedOrientation() method restart activity.

But why does this loop only occur on Kitkat (emulator?)? Testing Lollipop it does not happen

Nakamoto
  • 1,293
  • 14
  • 17

1 Answers1

0

I don't know why only occurs on KikKat, but I was able to fix removing handler use. Only TimerTask was need in my case.

This fixes that bug and prevent memory leaks.

   @Override
    protected void onStart() {
        super.onStart();

        timerTask = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        changeColors();
                    }
                });
            }
        };

        timer = new Timer();
        timer.schedule(timerTask, 0, speed);
    }

    @Override
    protected void onStop() {
        super.onStop();

        timerTask.cancel();
        timer.cancel();
        timer.purge();
    }
Nakamoto
  • 1,293
  • 14
  • 17