在方向改变时重新启动应用程序(Oncreate)
Restart Application(Oncreate) on change in orientation
我的应用程序中有一个计时器,如果方向在所有 4 个方向上都发生变化,我想重新启动计时器。我尝试通过添加 Onconfigurationchanged() 方法来实现,但它不为我调用和工作。
MainActivity.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("tag","oncreate changed");
setContentView(R.layout.activity_main);
timer = (AppCompatEditText)findViewById(R.id.editText);
totalHoursText = (TextView)findViewById(R.id.totalhoursText);
startButton = (Button)findViewById(R.id.button);
stopButton = (Button)findViewById(R.id.button1);
totalHoursText.setVisibility(View.INVISIBLE);
startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
handler = new Handler();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("tag", "orientation changed");
int orientation = newConfig.orientation;
switch (orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
Log.i("tag", "orientation changed landscape");
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button :
Time time = new Time();
time.setToNow();
flag=true;
startTime = time.toMillis(false);
handler.postDelayed(updateTimerThread,0);
totalHoursText.setVisibility(View.INVISIBLE);
break;
case R.id.button1:
timer.setText("00:00");
flag=false ;
handler.removeCallbacksAndMessages(updateTimerThread);
break;
}
}
private Runnable updateTimerThread = new Runnable() {
String str;
public void run() {
if(flag) {
Time time = new Time();
time.setToNow();
timeInMilliseconds = time.toMillis(false) - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
int sec = (int) (updatedTime / 1000) % 60;
int min = (int) ((updatedTime / (1000 * 60)) % 60);
int hr = (int) ((updatedTime / (1000 * 60 * 60)) % 24);
str = "" + String.format("%02d", hr) + ":"
+ String.format("%02d", min) + ":"
+ String.format("%02d", sec);
timer.setText("" + String.format("%02d", hr) + ":"
+ String.format("%02d", min) + ":"
+ String.format("%02d", sec));
handler.postDelayed(this, 0);
}
else{
totalHoursText.setVisibility(View.VISIBLE);
totalHoursText.setText(str);
}
}
};
请帮我提供建议或代码段。
尝试将 android:configChanges="orientation|screenSize"
这个 属性 添加到 AndroidManifest.xml
上的 activity
标签。之后 onConfigurationChanged
将在您的设备旋转时触发。
我的应用程序中有一个计时器,如果方向在所有 4 个方向上都发生变化,我想重新启动计时器。我尝试通过添加 Onconfigurationchanged() 方法来实现,但它不为我调用和工作。
MainActivity.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("tag","oncreate changed");
setContentView(R.layout.activity_main);
timer = (AppCompatEditText)findViewById(R.id.editText);
totalHoursText = (TextView)findViewById(R.id.totalhoursText);
startButton = (Button)findViewById(R.id.button);
stopButton = (Button)findViewById(R.id.button1);
totalHoursText.setVisibility(View.INVISIBLE);
startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
handler = new Handler();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("tag", "orientation changed");
int orientation = newConfig.orientation;
switch (orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
Log.i("tag", "orientation changed landscape");
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button :
Time time = new Time();
time.setToNow();
flag=true;
startTime = time.toMillis(false);
handler.postDelayed(updateTimerThread,0);
totalHoursText.setVisibility(View.INVISIBLE);
break;
case R.id.button1:
timer.setText("00:00");
flag=false ;
handler.removeCallbacksAndMessages(updateTimerThread);
break;
}
}
private Runnable updateTimerThread = new Runnable() {
String str;
public void run() {
if(flag) {
Time time = new Time();
time.setToNow();
timeInMilliseconds = time.toMillis(false) - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
int sec = (int) (updatedTime / 1000) % 60;
int min = (int) ((updatedTime / (1000 * 60)) % 60);
int hr = (int) ((updatedTime / (1000 * 60 * 60)) % 24);
str = "" + String.format("%02d", hr) + ":"
+ String.format("%02d", min) + ":"
+ String.format("%02d", sec);
timer.setText("" + String.format("%02d", hr) + ":"
+ String.format("%02d", min) + ":"
+ String.format("%02d", sec));
handler.postDelayed(this, 0);
}
else{
totalHoursText.setVisibility(View.VISIBLE);
totalHoursText.setText(str);
}
}
};
请帮我提供建议或代码段。
尝试将 android:configChanges="orientation|screenSize"
这个 属性 添加到 AndroidManifest.xml
上的 activity
标签。之后 onConfigurationChanged
将在您的设备旋转时触发。