Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8009271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:23:46+00:00 2026-06-04T18:23:46+00:00

When doing an orientation change in the emulator (ADT v17.0.0), or on a device

  • 0

When doing an orientation change in the emulator (ADT v17.0.0), or on a device (original Droid), I noticed that the activity will go through more than one create-destroy cycle sometimes. I found this blog post that mentions the problem, but offers no solution.

My app (API 8) currently does a ‘last-chance’ save of user data in onPause, and retrieves that data in onCreate/onStart, which works fine for a single restart, but gets into concurrency issues if the cycles come back-to-back. Specifically if a load starts before a save is complete, the ‘last-chance’ data is lost.

I did read the Faster Screen Orientation Change Android-developer article which mentions transferring an Object via onRetainNonConfigurationInstance/getLastNonConfigurationInstance. I tried using onRetain/getLast… like a flag to tell if the activity is “restarting”, which works, but I still have the main issue of not knowing if/when any existing save/load operations are completed.

Concurrency & thread-management are not my strongest suits, so I’m looking for a solution that does at least one save & load across any number of back-to-back restarts, without memory leaks. Since fast create-destroy cycles could happen for non-orientation reasons, ideas that just involve breaking/handling orientation changes alone aren’t really what I’m after.

Here’s a log file excerpt with some cycles, you can see where the activity gets created-destroyed twice when going from landscape to portrait. Here’s an excerpt of what I’ve got now:

onPause() {
    file_manager.saveTemporaryPattern(); // writes to OutputStream on UI thread
}

onStart() {
    findViewById (R.id.main_screen).post (new Runnable() {
        file_manager.loadTemporaryPattern(); // reads from InputStream on UI thread
    });

    if (getLastNonConfigurationInstance() != null) {
        // DO SOMETHING HERE?
    }
}

onRetainNonConfigurationInstance() {
    return dummy_object;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:23:48+00:00Added an answer on June 4, 2026 at 6:23 pm

    Managed to solve the concurrency issue by sending the Load/Save calls to a Handler thread linked to the Application context, rather than the Activity context. Had to setup a Load vs Save token in the calls to check for a Save->Load->Save pattern, avoiding the odd behavior of getting an incomplete activity lifecycle on a landscape->portrait orientation change.

    It works in development & on test devices, and doesn’t leak the activity context, so we’ll see how it does in the wild.

    In YourApp extends Application:

    private Handler fileAccessThread = null;
    
    public void onCreate() {
        super.onCreate();
    
        if (fileAccessThread == null) {
            fileAccessThread = new Handler();
            token = 0;
        }
    }
    
    public void postCallbackFileAccess (int _token, Runnable _callback) {
        switch (_token) {
        case TOKEN_SAVE:
            // Save must follow load, not another save
            if (token == TOKEN_SAVE) { return; }
            token = _token;
            break;
    
        case TOKEN_LOAD:
    //      Have to allow load->load, otherwise data gets lost/deleted
    //      if (token == TOKEN_LOAD) { return; }
            token = _token;
            break;
        }
    
        fileAccessThread.post (_callback);
    }
    

    In YourActivity:

    protected void onStart() {
        super.onStart();
    
        ((YourApp) getApplication()).postCallbackFileAccess (
            YourApp.TOKEN_LOAD, new Runnable() {
                @Override
                public void run() {
                    file_manager.load();
                }
        });
    }
    
    protected void onPause() {
        super.onPause();
    
        ((YourApp) getApplication()).postCallbackFileAccess (
            YourApp.TOKEN_SAVE, new Runnable() {
                @Override
                public void run() {
                    file_manager.save();
                }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hooked up to the orientation change notifications. I noticed that for a 90
So I wam trying to forcefully change the device orientation at a part of
I have a TabActivity that I am having problems after the device changes orientation.
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
I have a semi-complicated problem and hoping that someone here will be able to
We have an iPhone app that is doing a lot of rotation between portrait
I'm attempting to save a ListFragment subclass across an orientation change so I added
Possible Duplicate: Check orientation on Android phone I am writing an app that requires
Being frustrated about fragment behavior, I started doing some testing. I have one activity
I would like to change the orientation of my SurfaceView from landscape to portrait.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.