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 6931451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:34:44+00:00 2026-05-27T11:34:44+00:00

I have an app which has a main activity that calls other activities on

  • 0

I have an app which has a main activity that calls other activities on a button press.

The main activity hosts a class which contains a lot of data and is parcelable. the data is the backbone for the application and I need to save it across orientation changes. In order to achieve this it use onSaveInstanceState and put the parcelable object in the bundle.

However, I have noticed that onSaveInstanceState is called everytime i press a button and start one of the new activities as well as on orientation changes. This is slightly slowing down the transition to the other screens.

Is there a good way to only store this information when I know for definite that the activity will be destroyed rather than every time I navigate away from the Activity periodically. I understand that the minute I navigate away from the main activity there is a chance that it may get destroyed.

Thanks,
M<

  • 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-05-27T11:34:45+00:00Added an answer on May 27, 2026 at 11:34 am

    For primitive values, you should use onSaveInstanceState. For restoring you can use onRestoreInstanceState or you can some code in onCreate like this:

    if(savedInstanceState != null) {
      // restore old state
    } else {
      // a fresh start
    }
    

    Now for restoring big objects like Bitmap etc if they are not expensive to create and doesn’t make your UI sluggish, create them again on restore. If you do not want to that then use onRetainNonConfigurationInstance and code will look like this:

    @Override
    public Object onRetainNonConfigurationInstance () {
      return bmp;
    }
    
    @Override
    public void onCreate() {
    
      bmp = (Bitmap)getLastNonConfigurationInstance();
    }
    

    WARNING: This api is deprecate, you might use it on old platforms. I put it here for illustration purpose. The new way to do this is more involving.

    Here is detailed ref:

    getLastNonConfigurationInstance

    onRetainNonConfigurationInstance

    Recommended soln for new api levels 11 and above:

    “Use the new Fragment API setRetainInstance(boolean) instead; this is also available on older platforms through the Android compatibility package.” snippet from the doc when you click on the links above.

    So, you will create your bitmaps inside the fragment and call setRetainInstance on fragment so that fragment and its content (in your case bitmap) will be retained accross config change.

    Ref:

    setRetainInstance

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that has a list as its main activity and then
I have an app that goes between many different activities. One activity, my main
I have two activities. One activity has the main game and the other activity
I have an application that contains two Activities with <intent-filter> <action android:name=android.intent.action.MAIN/> <category android:name=android.intent.category.LAUNCHER/>
I have an app with 2 activities, the preference and the main activity, I
I have a main activity, that holds the main menu. This menu has an
i have a main Activity called Main which has onActivityResult Method. protected void onActivityResult(int
I have a .net winforms app which has a few animation effects, fade ins
I have an view in my App which has a number of buttons based
I have a user profile page on my web app which has contact information.

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.