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
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();
}
Related
How to do sample counter in Activity? This is not working.
public class MainActivity extends AppCompatActivity implements Runnable {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
run();
}
#Override
public void run() {
while (true) {
updateTv();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void updateTv() {
int counter = 100;
final TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(String.valueOf(counter));
counter--;
}
}
In onCreate() you're starting an infinite loop inside of the UI thread, blocking it completely. Alternatively you could use a Handler for periodic updates. Maybe using a bigger delay and stop it sometime.
public class MainActivity extends AppCompatActivity implements Runnable {
private final Handler mHandler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
run();
}
#Override
public void run() {
updateTv();
mHandler.postDelayed(this, 17);
}
public void updateTv() {
int counter = 100;
final TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(String.valueOf(counter));
counter--;
}
}
Anyway you should read What is the Android UiThread (UI thread) for sure.
Consider using Timer class which allows you to define a callback method that will be invoked at specified rate.
An example that fits your needs:
public class CounterActivity extends AppCompatActivity {
private TextView mCounterTextView;
private Timer mTimer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_counter);
mCounterTextView = (TextView) findViewById(R.id.counterTextView);
mTimer = new Timer();
mTimer.scheduleAtFixedRate(
new CounterTask(100), 0, TimeUnit.SECONDS.toMillis(1));
}
protected class CounterTask extends TimerTask {
protected int mCounter;
CounterTask(int initial) {
mCounter = initial;
}
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
mCounterTextView.setText(String.valueOf(mCounter));
}
});
--mCounter;
}
}
}
One more thing that should be noticed. As Timer executes it's own thread - it prevents you from updating your UI from outside of the main thread. In that case
you have to register a Runnable using runOnUiThread method.
Also, calling findViewById in a loop is not the best idea.
When I run the app and press the back button neither onPause(), onStop() or onDestroy() stop the time_counter thread, which results in endless messages "elTime: " + elapsedTime.
public class GameBoard extends Activity {
DrawingView drawView;
Thread timer_thread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
setContentView(R.layout.activity_game_board);
DrawingView drawingView = (DrawingView)findViewById(R.id.drawing_view);
drawingView.setInitialParameters(size.x,size.y);
TextView time_counter_tv = (TextView)findViewById(R.id.time_counter);
runTimer(time_counter_tv);
}
private void runTimer(final TextView elapsedTimeTV) {
timer_thread = new Thread() {
public void run() {
final long startTime = SystemClock.elapsedRealtime();
while (!Thread.currentThread().isInterrupted()) {
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
long elapsedTime = SystemClock.elapsedRealtime() - startTime;
System.out.println("elTime: " + elapsedTime);
elapsedTimeTV.setText("Elapsed Time: " + elapsedTime/100 + "s");
}
});
timer_thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
timer_thread.start();
}
#Override
protected void onPause(){
super.onPause();
System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-----------------");
timer_thread.interrupt();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
Also in DrawingView class, when trying to finish the activity the above methods do not stop the thread either.
public class DrawingView extends View {
public void doSth(){
if (time_has_come){
((Activity)getContext()).finish();
return;
}
}
I have a clock done by updating a TextView text using a Runnable. When I'm in the Activity the TextView is updated properly, but when I leave and come back to the activity, the code in the run() method is not executed anymore.
Do I have to call run() again in the onResume of my activity? Why? Is the mTicker Runnable stopped?
MyActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler();
mTicker = new Runnable() {
public void run() {
if(mClockStopped) return;
long now = System.currentTimeMillis();
mCalendar.setTimeInMillis(now);
mClock.setText(DateFormat.format("kk:mm", mCalendar));
mClock.invalidate();
long upTime = SystemClock.uptimeMillis();
long next = upTime + (60000 - now % 60000);
mHandler.postAtTime(mTicker, next);
}
};
mTicker.run();
/* more stuff */
}
#Override
public void onResume()
{
super.onResume();
mClockStopped = false;
}
#Override
public void onPause()
{
mClockStopped = true;
super.onPause();
}
Maybe is not that simple, but setting mClockStopped to true make your runnable's run() exit.
You should call
mClockStopped = false;
mTicker.run();
In onResume
I have been running through alot of issues try to pause and unpause a timer, and if I lock the orientation to portrait or landscape it works, but thats not exactly what I want to do. Of course, the onCreate method is called when you change orientation, so im canceling my timertask and setting it to null, but after running through the orientation more than once, it doesnt cancel the timertask anymore. Ive looked through other peoples questions on here but none seem to hold the answer to my quesiton. Heres my code. Its a little sloppy at the moment because ive been trying about everything I can to get it to work.
public class singleTimer extends Activity implements OnClickListener {
private Integer setTime = 0;
private Integer tmrSeconds = 0;
private Integer tmrMilliSeconds = 0;
private Timer myTimer = new Timer();
private TimerTask myTimerTask;
private TextView timerText;
private boolean isPaused = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_timer);
Bundle extras = getIntent().getExtras();
setTime = extras.getInt("com.bv.armyprt.timer_duration");
if (myTimerTask != null) {
myTimerTask.cancel();
myTimerTask = null;
}
if (savedInstanceState != null) {
if (savedInstanceState.getInt("tmrSeconds") == 0) {
tmrSeconds = setTime;
} else {
tmrSeconds = savedInstanceState.getInt("tmrSeconds");
tmrMilliSeconds = savedInstanceState.getInt("tmrMilliseconds");
if (isPaused == false) {
myTimer = new Timer();
myTimerTask = new TimerTask() {
#Override
public void run() {
TimerMethod();
}
};
myTimer.schedule(myTimerTask, 0, 100);
}
}
} else {
tmrSeconds = setTime;
}
timerText = (TextView)findViewById(R.id.timerText);
timerText.setText(String.format("%03d.%d", tmrSeconds, tmrMilliSeconds));
TextView timerDesc = (TextView)findViewById(R.id.timerDescription);
timerDesc.setText("Timer for: " + setTime.toString());
Button startButton = (Button)findViewById(R.id.timerStart);
Button stopButton = (Button)findViewById(R.id.timerStop);
Button closeButton = (Button)findViewById(R.id.timerClose);
closeButton.setOnClickListener(this);
startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case (R.id.timerStart):
isPaused = false;
myTimer = new Timer();
myTimerTask = new TimerTask() {
#Override
public void run() {
TimerMethod();
}
};
myTimer.schedule(myTimerTask,0, 100);
break;
case (R.id.timerStop):
isPaused = true;
myTimerTask.cancel();
myTimerTask = null;
myTimer.cancel();
break;
case (R.id.timerClose):
onDestroy();
this.finish();
break;
}
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
this.
tmrMilliSeconds--;
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.
if (tmrSeconds > 0) {
if (tmrMilliSeconds <= 0) {
tmrSeconds--;
tmrMilliSeconds = 9;
}
} else {
Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
myTimer.cancel();
tmrSeconds = setTime;
tmrMilliSeconds = 0;
isPaused = true;
}
//Do something to the UI thread here
timerText.setText(String.format("%03d.%d", tmrSeconds, tmrMilliSeconds));
}
};
#Override
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putInt("setTimer", setTime);
savedInstanceState.putInt("tmrSeconds", tmrSeconds);
savedInstanceState.putInt("tmrMilliseconds", tmrMilliSeconds);
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
setTime = savedInstanceState.getInt("setTimer");
tmrSeconds = savedInstanceState.getInt("tmrSeconds");
tmrMilliSeconds = savedInstanceState.getInt("tmrMilliSeconds");
}
}
you can simply add a boolean variable
boolean stopTImer = false ;
and in your timerTask , do something like this :
#Overrride
public void run(){
if(!stopTimer){
//do stuff ...
//...
}
and when you want to stop it , put the boolean to true
You should stop the timer during onStop. Android might create another instance of your Activity and you will lose the reference to your previous timer(task) when you change orientation.
All objects tied to an activity follow the activity lifecycle. That means you have to store the references to objects elsewhere if you want to keep them even if the activity gets deleted (which can happen quite often).
I created a splash screen using the follow code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.splash_layout);
Thread splashThread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while (_active && (waited < _splashTime)) {
sleep(100);
if (_active) {
waited += 100;
}
}
} catch (InterruptedException e) {
// do nothing
} finally {
_active = false;
finish();
startActivity(new Intent(SplashActivity.this, MyMainActivity.class));
}
}
};
splashThread.start();
}
there is an image view in splash_layout, after the splash screen appears for some time duration, and disappears then MyMainActivity starts, the problem is, after the splash disappears and before MyMainActivity starts, I could see previous screen(irrelevant to my app, e.g. desktop with widgets, or previous running app), how to make the transition fluent so that splash screen directly goes to MyMainActivity?
Thanks!
Can you try this i am not sure this is 100% work but try may be helpful..
protected int _splashTime = 3000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
finish();
startActivity(new Intent(SplashActivity.this, MyMainActivity.class));
}
}, _splashTime);
}
Try calling finish() after startActivity().
private static final long SPLASH_SCREEN_MS = 2500;
private long mTimeBeforeDelay;
private Handler mSplashHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
// Create a new Handler.
mSplashHandler = new Handler();
}
#Override
protected void onResume() {
super.onResume();
// The first time mTimeBeforeDelay will be 0.
long gapTime = System.currentTimeMillis() - mTimeBeforeDelay;
if (gapTime > SPLASH_SCREEN_MS) {
gapTime = SPLASH_SCREEN_MS;
}
mSplashHandler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
SplashScreenActivity.this.finish();
}
}, gapTime);
// Save the time before the delay.
mTimeBeforeDelay = System.currentTimeMillis();
}
#Override
protected void onPause() {
super.onPause();
mSplashHandler.removeCallbacksAndMessages(null);
}
For your reference, here is a best example for Android - Splash Screen example.
You can try this code:1
public class MainActivity extends Activity {
private ImageView splashImageView;
boolean splashloading = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
splashImageView = new ImageView(this);
splashImageView.setScaleType(ScaleType.FIT_XY);
splashImageView.setImageResource(R.drawable.ic_launcher);
setContentView(splashImageView);
// interesting music
/**
* Gets your sound file from res/raw
*/
splashloading = true;
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
splashloading = false;
setContentView(R.layout.activity_main);
}
}, 3000);
}
Best of luck!