Reputation: 51
I'm working on an Android App and dealing with an issue !!
When I change the screen from the portait mode to the Landscape mode without changing the activity I'm working on android Stop, Destroy and then create and launch my Activity.
To know that, I printed the times OnCreate(),onStart(), onRestart() ... are called.
What I want to do is to stop counting when I'm just changing the screen mode or don't kill and distroy my activity, when I change the screen mode.
Here is my code
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);
}
}
Upvotes: 0
Views: 153
Reputation:
I don't have enough reputation to comment, but if I understand you correctly, you can ignore screen orientation if you:
onResume()
method.onPause()
method.This way, you don't have to worry about screen orientation as onPause
is called whenever orientation started i.e. the device is physically rotated. While onResume()
is called when orientation is finished.
Upvotes: 1
Reputation: 15668
Now a quick fix is to add this line to AndroidManifest.xml where you see your activity
android:configChanges="orientation|screenSize"
And in your Activity override the following method
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
Now your activity will not be destroyed
Upvotes: 0