我正在开发Android应用并处理问题!!
当我将屏幕从肖像模式更改为横向模式而不更改活动时,我正在使用Android停止,销毁,然后创建并启动我的活动。
要知道,我打印的时间是OnCreate(),onStart(),onRestart()...被调用。
当我改变屏幕模式时,我想做的是在我只是更改屏幕模式时停止计数,或者不要杀死和破坏我的活动。
这是我的代码
public class ActivityX extends Activity {
private static final String RESTART_KEY = "restart";
private static final String RESUME_KEY = "resume";
private static final String START_KEY = "start";
private static final String CREATE_KEY = "create";
// String for LogCat documentation
private final static String TAG = "ActivityX";
// Lifecycle counters
private int mCreate = 0;
private int mRestart = 0;
private int mStart = 0;
private int mResume = 0;
//Create variables for each of the TextViews
private static int mTvCreate = 0;
private static int mTvRestart = 0;
private static int mTvStart = 0;
private static int mTvResume = 0;
TextView startTxtView ;
TextView restartTxtView;
TextView resumeTxtView;
TextView createTxtView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
startTxtView = (TextView) findViewById(R.id.start);
restartTxtView = (TextView) findViewById(R.id.restart);
resumeTxtView = (TextView) findViewById(R.id.resume);
createTxtView = (TextView) findViewById(R.id.create);
Button launchActivityYButton = (Button) findViewById(R.id.bLaunchActivityY);
launchActivityYButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try{
Intent intentActivity = new Intent(ActivityX.this, ActivityY.class);
startActivity(intentActivity);
} catch (Exception e) {
// Log any error messages to LogCat using Log.e()
Log.e(TAG, "On click X not working");
}
}
});
// Has previous state been saved?
if (savedInstanceState != null ) {
mCreate = savedInstanceState.getInt(CREATE_KEY);
mStart = savedInstanceState.getInt(START_KEY);
mRestart = savedInstanceState.getInt(RESTART_KEY);
mResume = savedInstanceState.getInt(RESUME_KEY);
}
// Emit LogCat message
Log.i(TAG, "Entered the onCreate() method");
mTvCreate = mCreate + 1;
mTvStart = mStart;
mTvRestart = mRestart;
mTvResume = mResume;
createTxtView.setText("onCreate()calls: "+mTvCreate);
startTxtView.setText("onStart()calls: "+mTvStart);
resumeTxtView.setText("onResume()calls: "+mTvResume);
restartTxtView.setText("onRestart()calls: "+mTvRestart);
}
@Override
public void onStart() {
super.onStart();
// Emit LogCat message
Log.i(TAG, "Entered the onStart() method");
mTvStart = mTvStart + 1;
this.displayCounts();
}
@Override
public void onResume() {
super.onResume();
// Emit LogCat message
Log.i(TAG, "Entered the onResume() method");
mTvResume = mTvResume + 1;
this.displayCounts();
}
@Override
public void onPause() {
super.onPause();
// Emit LogCat message
Log.i(TAG, "Entered the onPause() method");
}
@Override
public void onStop() {
super.onStop();
// Emit LogCat message
Log.i(TAG, "Entered the onStop() method");
}
@Override
public void onRestart() {
super.onRestart();
// Emit LogCat message
Log.i(TAG, "Entered the onRestart() method");
mTvRestart = mTvRestart + 1;
this.displayCounts();
}
@Override
public void onDestroy() {
super.onDestroy();
// Emit LogCat message
Log.i(TAG, "Entered the onDestroy() method");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt(CREATE_KEY, mTvCreate);
savedInstanceState.putInt(RESTART_KEY, mTvRestart);
savedInstanceState.putInt(START_KEY, mTvStart);
savedInstanceState.putInt(RESUME_KEY, mTvResume);
super.onSaveInstanceState(savedInstanceState);
}
public void displayCounts() {
createTxtView.setText("onCreate() calls: " + mTvCreate);
startTxtView.setText("onStart() calls: " + mTvStart);
resumeTxtView.setText("onResume() calls: " + mTvResume);
restartTxtView.setText("onRestart() calls: " + mTvRestart);
}
}
答案 0 :(得分:1)
我没有足够的声誉来评论,但如果我理解正确,您可以忽略屏幕方向:
onResume()
方法开始计算。onPause()
方法计数。这样,您就不必担心屏幕方向,因为只要方向开始就调用onPause
,即设备是物理旋转的。在方向完成时调用onResume()
。
答案 1 :(得分:0)
现在快速解决方法是将此行添加到您看到活动的AndroidManifest.xml
android:configChanges="orientation|screenSize"
并在您的Activity中覆盖以下方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
现在你的活动不会被销毁