onDestroy gets called each time the screen goes on

23,852

Solution 1

If you want to stop the destroy/create issue that is the default in android because of an orientation change and lock in one orientation then you need to add code and xml

In your activites code (notes about the xml)

    // When an android device changes orientation usually the activity is destroyed and recreated with a new 
    // orientation layout. This method, along with a setting in the the manifest for this activity
    // tells the OS to let us handle it instead.
    //
    // This increases performance and gives us greater control over activity creation and destruction for simple 
    // activities. 
    // 
    // Must place this into the AndroidManifest.xml file for this activity in order for this to work properly 
    //   android:configChanges="keyboardHidden|orientation"
    //   optionally 
    //   android:screenOrientation="landscape"
    @Override
    public void onConfigurationChanged(Configuration newConfig) 
    {
        super.onConfigurationChanged(newConfig);
    }

Solution 2

I had same issue. From ActivityA I called ActivityB. When I closed ActivityB, I expected ActivityA to be shown but it wasn't - it was destroyed. This issues was caused by following attributes of ActivityA in AndroidManifest.xml:

android:excludeFromRecents="true"
android:noHistory="true"

Because of them ActivityA was destroyed after ActivityB was started.

Solution 3

mikepenz,in your case if you realy need a android:setorientation = "landscape" means ,you dont need to remove it, just add these set of attribute android:configchanges = "orientation|Screensize" this wont destroy your activity... hope this helps you.

Share:
23,852

Related videos on Youtube

mikepenz
Author by

mikepenz

Updated on July 09, 2022

Comments

  • mikepenz
    mikepenz almost 2 years

    My application gets killed each time that it comes back from the screen-off-state. I fetch all the information that my app does, but I can't find out why it calls onDestroy. It's the first time I'm seeing this behavior in my applications.

    My main activity extends tabActivity because it contains a tabhost. I've read that it has to extend it or it will FC. I'm not sure if my issue is related to this?! Oh and it implements Observer but this should be no problem.

    Here are the logs:

    07-21 09:57:53.247: VERBOSE/###(13180): onResume
    07-21 09:57:53.267: VERBOSE/###(13180): onPause
    07-21 09:57:59.967: VERBOSE/###(13180): onResume
    07-21 09:58:00.597: VERBOSE/###(13180): onPause
    07-21 09:58:00.597: VERBOSE/###(13180): onDestroy
    07-21 09:58:00.637: VERBOSE/###(13180): onCreate
    

    The crazy thing is that it calls the onDestroy the most times after the screen goes on again, and sometimes it has enough time to do this before the screen goes off. But after it goes on again it does the same again...

    I hope that someone has a tip for me or any information on how to resolve this issue.

    I'm not sure if this is important, but I use the android 2.1-update1 sdk for my application.


    EDIT:

    The application gets tested on a real Android Device.

    Here is some basic code with all unnecessary lines and information removed:

    package;
    imports;
    
    public class WebLabActivity extends TabActivity implements Observer{
    
    #declerations
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v("###", "onCreate");
        setContentView(R.layout.main);
        # initialize some basic things
    }
    
    @Override
    public void onResume() {
        super.onResume();
        Log.v("###", "onResume");
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.v("###", "onDestroy");
    }
    
    @Override
    public void onRestart() {
        Log.v("###", "onRestart");
        super.onRestart();
    }
    
    @Override
    public void onPause() {
        Log.v("###", "onPause");
        super.onPause();
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Log.v("###", "onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
    }
    
    @Override
    public void update(Observable observable, Object data) {
        Log.v("###", "notifyManager.getWho() + " made an Update");
    }
    
    
        private void initializeSidebarTabhost() {
        TabSpec 1 = tabHost.newTabSpec("1");
            TabSpec 2 = tabHost.newTabSpec("2");
        TabSpec 3 = tabHost.newTabSpec("3");
        TabSpec 4 = tabHost.newTabSpec("4");
    
    
        1.setIndicator("###");
        2.setIndicator("###");
        3.setIndicator("###");
        4.setIndicator("###");
    
        addIntents
    
        tabHost.addTab(1); //0
        tabHost.addTab(2); //1
        tabHost.addTab(3); //2
        tabHost.addTab(4); //3
    
        tabHost.getTabWidget().setCurrentTab(2);
    }
    }
    

    EDIT2:

    Ok, I've tested my application without initializing anything, then with only extending activity, or without implementing observer, but my changes had no effect. Every time I set my phone to sleep, then wake it up, onDestroy() get's called?!


    EDIT3:

    Ok, I found out something interesting.

    First here's my AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.tundem.###"
          android:versionCode="1"
          android:versionName="1.0">
        <uses-sdk android:minSdkVersion="7" />
    
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".###" android:label="@string/app_name" android:screenOrientation="landscape" android:theme="@android:style/Theme.Light.NoTitleBar">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    </manifest>
    

    As soon as I remove the screenOrientation="landscape", the application won't be destroyed each time that I wake up my device. I tried it more than 10 times but no more calls to onDestroy()

    So I think that I will have to set this in code?! Any tips or pieces of code?

    • Idistic
      Idistic almost 13 years
      That sounds kind of weird, is this in the emulator, and can you post a skelton code framework that exhibits the behavior?
    • mikepenz
      mikepenz almost 13 years
      On the emulator i havn't tried it yet. I get this behavior on my real Android phone so no emulator. What do you mean with which exhibits the behavior. I will post some code in the next few minutes.
    • Idistic
      Idistic almost 13 years
      I mean some code that verifiably reproduces the problem, sometimes people put up snippets that leave out the real issue
    • mikepenz
      mikepenz almost 13 years
      i put up the code i think is important, because if i overlook the things i left here with my samplecode i'm sure that these things doesn't cause the problem. Or do you think that there could be a problem with addListeners. Open a file on update, call the method initizialSidebarTabhost, and call addObserver?! i think the problem is caused by extending the TabActivity, but do you know any other trick how i can use a tabhost?!
    • denis.solonenko
      denis.solonenko almost 13 years
      @mikepenz it is weird. but somehow good too - you cannot control when android os destroy or re-creates your app, so you should make your app immune to this behavior. you never know what could happen on a real-device-with-a-manufacturer-custom-android-build :)
    • mikepenz
      mikepenz almost 13 years
      I know that i can't control this behavior. I made a variable called calledOnce, which will be setted to true after calling the onCreate once, but the activity is completly resetted each time. so it loads everything new, and this is really weird, because i will do some timeintensive operations and if they will be done each time again this will only drain the battery and make the users angry if they have to wait again.
  • mikepenz
    mikepenz almost 13 years
    i found this solution on my own a few minutes ago. Because i have to less reputation i was not able to post it here. but thank you a lot for the answer ;) I hope it is useful for all the others out there
  • Idistic
    Idistic almost 13 years
    @mikepenz - glad discussion led you to your own solution, cheers
  • mikepenz
    mikepenz almost 13 years
    Yes it's great if you find the answer alone ;). And it's great to get help from others. So thanks again for your help. I'm sure i would have needed hours to find the same solution. ;). But you were right, sometimes or often people forget about the main points which causes a problem then in their main question. Thanks again ;)
  • tmandry
    tmandry almost 12 years
    If you are targeting API version 13 or above, you also need to add |screenSize to the list of configChanges in the manifest. developer.android.com/guide/topics/resources/…
  • tmandry
    tmandry almost 12 years
    Also, you don't really need to add the Java code if all you're doing is calling the super.onConfigurationChanged() function that would be called anyway.
  • GAMA
    GAMA over 10 years
    Bit strange, but I didn't faced the issue mentioned in question for device with 2.3 OS. But I found this issue for same app on device with 4.0 OS. But this solution worked like a charm ! +1...
  • Kevin
    Kevin over 9 years
    FYI it won't work this way, need: (note the lower case s, then capital S in screensize) android:configChanges="keyboardHidden|orientation|screenSize‌​"
  • ToolmakerSteve
    ToolmakerSteve over 8 years
    As tmandry says, you don't need any code. See johny001's more recent answer, for the XML line that is all you need (but read Kevin's comment for the correction).
  • ToolmakerSteve
    ToolmakerSteve over 8 years
    Different issue. But useful.
  • Michael
    Michael over 7 years
    so is it the actual orientation change that is causing it? i see this happen sometimes (I have screenOrientation="portrait") but it's random, doesn't happen all the time, and doesn't happen if I just change the orientation of the device. How can I tell which config change is causing this?