0

In my app, I use the onCreate() method to initialize various variables. However, whenever I rotate the device, and the screen auto-rotates, onCreate() is called again, which re-initialize my variables. Is that how it's supposed to work? Where should I put code that I only want to be run once, when I start the app?

JB_User
  • 3,117
  • 7
  • 31
  • 51
  • `onCreate()` is indeed called when the screen rotates. –  Apr 06 '13 at 18:47
  • read here http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/ or here http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android – Elior Apr 06 '13 at 18:48
  • Can I prevent the screen from auto-rotating? I tried doing that in the Devices 'Settings' app, but then it locked it into landscape mode. I need Portrait mode. Can I lock this device into Portrait mode and prevent autorotates? – JB_User Apr 06 '13 at 18:49
  • I read the stackoverflow link. I tried creating 'class MyApplicationClass extends Application' but the onCreate() method in that class never gets called when I start the app. – JB_User Apr 06 '13 at 18:56

2 Answers2

1

The above answers will lock your Activity into a specific orientation, which is generally not proper behavior for an Android app.

What you should be doing is storing your activity's state so when it gets recreated you can repopulate the UI with the stored values.

protected void onSaveInstanceState (Bundle outState) {
   super.onSaveInstanceState(outState);
   // put your values in the Bundle
   outState.putString("TextView1Text", textView1.getText()); // for example;
}

Then in your onCreate() method you can restore the values

protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        textView.setText(savedInstanceState.getString("TextView1Text"));
    }
}

This will also work when the user leaves the app via the home button or other means.

Brett Duncavage
  • 2,163
  • 14
  • 16
  • I appreciate your answer. I think it might be the 'right' way to do things, and I'll store it away for later use. For this app, however, I only need portrait mode. – JB_User Apr 06 '13 at 19:40
0

In the manifest put this line in all the activities that you want PORTRAIT

android:screenOrientation="portrait" 
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • That works. Thanks. However it still calls onCreate() if I switch between the app and the home screen. – JB_User Apr 06 '13 at 19:06
  • This sounds like a simple question, but I can't figure it out. How can I completely quit an app so that when I restart it, none of the variables have been initialized? – JB_User Apr 06 '13 at 19:09
  • What do you mean? when you press the HOME button and then launch the app again? – Hoan Nguyen Apr 06 '13 at 19:09
  • This really isn't a solution to your problem, you should support both orientations whenever possible. – Brett Duncavage Apr 06 '13 at 19:10
  • @Brett Duncavage Sometimes the other orientation does not make sense. Look at your phone contacts, it is portrait only. – Hoan Nguyen Apr 06 '13 at 19:15
  • Put android:clearTaskOnLaunch="true" in your main activity manifest. – Hoan Nguyen Apr 06 '13 at 19:17
  • That's why I said "whenever possible" sometimes it is better not to support both orientations, that's why android gives you a way to do it. Your question was about onCreate and restoring the view state after multiple calls. – Brett Duncavage Apr 06 '13 at 19:19
  • @Brett Duncavage look at the third comment after the question. – Hoan Nguyen Apr 06 '13 at 19:20
  • This is a tablet, not a phone. And for this app, only portrait mode makes sense. But I agree, I would like to know the right way to solve it. – JB_User Apr 06 '13 at 19:32
  • Hoan: if I'm running the app, then click the 'back' button to get to the home screen, or whatever, then later press the icon for the app, then it calls onCreate() again. I know (now) that's how it is intended to work. But it isn't really how I want it to work. – JB_User Apr 06 '13 at 19:33
  • It's true that some apps just make more sense to remain in one orientation over another but this answer is not the proper solution. Brett's answer is the more correct manner to handle this problem. An orientation change is not the only event that will cause an activity to be recreated such as returning to an activity that the system has destroyed. – TJ Thind Apr 06 '13 at 20:24
  • @JB_User If you want to retain the state of your app when the user back press then try this android:alwaysRetainTaskState="true" for your main activity. You may need to call finish() for other activities when the user back press. – Hoan Nguyen Apr 06 '13 at 21:51