3

I have read some blogs and visited some site. I want to know which event fired only one time during the life cycle. After reading blogs I realize that onCreate() method is fired only once during the life cycle. I don't know I am right or wrong. Now my problem is that I want to fired any event which fired one time only if I change the landscape orientation or portrait orientation than this event not fired means after starting activity if user change the orientation than event not fired. I want to know which event fired only one time after starting Activity. this onCreate()

Bhavin Chauhan
  • 1,950
  • 1
  • 26
  • 47

3 Answers3

8

enter image description here

onCreate and onDestroy id fired only once.

onCreate: Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc...

onDestroy: The final call you receive before your activity is destroyed. This can happen either because the activity is finishing.

so, put your event code in onCreate.(but its depend on your requirement what you are trying to do your code may be change)


Activity Flow:

first onCreate is called --> Next --> onStart --> onResume --> your Activity is Running is show you your layout. (whatever you have put in your layout.xml)

now if you Press HOME Button then its goes to --> onPause --> onStop. (Activity is not Destroy its running in background). now again open Activity its go to --> onRestart --> onStart --> onResumme(activity is running again).

now if you Press Back Button then --> onPause --> onStop --> onDestroy.


Edited:

to stop restart activity when orientation change use

android:configChanges="orientation|keyboardHidden" in android manifest file.

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">

if you developing for API level 13 or higher you must use

android:configChanges="orientation|screenSize"
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • 1
    thanks Dhawal Sodha for answer I mention in question that I know that onCreate() method fired one time but now my problem is that if i change my orientation than this event fired again.. – Bhavin Chauhan Apr 30 '13 at 06:04
  • 1
    Dhawal Sodha your answer is not completely but may be this help some one else also!! – Bhavin Chauhan Apr 30 '13 at 16:21
4

Make a dummy application, override onCreate, onStart, onResume, onPause, onDestroy, onRestart put Log.d("MYAPP", "onXXX called") in there and see for yourself what and in which order gets called.

This way you learn things practical way once and for all.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
0

You can make use of preferences in onCreate().

SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        if (!prefs.getBoolean("firstTime", false)) {

// run your one time code


            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("firstTime", true);
            editor.commit();
        }
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
  • Probably the preference should be reset when the user exits the app, because it persists throughout app lifes. But there's no good place to do that.. – Alexander Kulyakhtin Apr 30 '13 at 06:08
  • thanks shree202 for answer but if I stored value in SharedPreferences than if user start second time application(not just activity) that it couldn't give me the best output.. – Bhavin Chauhan Apr 30 '13 at 06:14
  • Why ?? whats the problem ? please describe it. – Chintan Soni Apr 30 '13 at 06:16
  • @Alex preferences are never meant for reset. Think, if you reset the preferences when app exists, then what is the use of it...! Preferences are meant for persistance of values you set. – Chintan Soni Apr 30 '13 at 06:19
  • shree202 if in my activity i wrote your code in onCreate() event and stored the firstTime value to true after my code execute after finished this activity if user start the activity again than SharedPreferences value is already true and my code will not execute. – Bhavin Chauhan Apr 30 '13 at 06:27
  • in that case, can you please explain clearly and briefly what you want to achieve ? – Chintan Soni Apr 30 '13 at 06:36
  • @shree202 I have one activity in which i have 2 different layout design, I have one method to binding some information. and I want to bind only one time when the activity stared. means if user change the orientation than this method must not fired. in your solution if user start application and he/she visit this activity after that if user forcefully closed application and stared again than SharedPreferences have already value true so than my method will not run.. – Bhavin Chauhan Apr 30 '13 at 06:51
  • Please post your code. Tha activity in which you are willing to achieve this mechanism. – Chintan Soni Apr 30 '13 at 06:57
  • Means you're doing some task with AsyncTask and you don't want that task to be repeated when orientation gets chnged. Am i right ?? – Chintan Soni Apr 30 '13 at 07:16
  • @shree202 how can I solve my problem? is there any idea in your mind? – Bhavin Chauhan Apr 30 '13 at 07:24
  • Check this http://stackoverflow.com/questions/1111980/how-to-handle-screen-orientation-change-when-progress-dialog-and-background-thre – Chintan Soni Apr 30 '13 at 07:29
  • @Bhavin Chauhan Did the above link i commented, helped you ? No totally but to some extent...?? – Chintan Soni Apr 30 '13 at 07:40
  • your posted link is too good.. and may be this solved my problem of crashing my app not my posted problem!! – Bhavin Chauhan Apr 30 '13 at 07:42
  • 1
    check http://stackoverflow.com/questions/7842691/handle-states-of-orientation-changes, http://stackoverflow.com/questions/2967903/handling-orientation-changes-yourself – Chintan Soni Apr 30 '13 at 07:48
  • 1
    Also check http://stackoverflow.com/questions/8253735/handling-orientation-changes-android, http://stackoverflow.com/questions/2988072/problems-when-handling-orientation-changes, http://stackoverflow.com/questions/14685011/handle-orientation-change-with-running-asynctask – Chintan Soni Apr 30 '13 at 07:52
  • Ok then, mark this answer accepted accepted, post your correct code how you found the solution to your problem so that others can learn from you and keep working like this. Keep it up. – Chintan Soni Apr 30 '13 at 13:24
  • I actually feel happy that i could help you, atlast. – Chintan Soni Apr 30 '13 at 13:25