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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:50:47+00:00 2026-05-20T08:50:47+00:00

In my activity I want to use the ‘onRetainNonConfigurationInstance’ method to store some of

  • 0

In my activity I want to use the ‘onRetainNonConfigurationInstance’ method to store some of the data I’ve loaded in the activity (no views). This should speed up loading and keep a consistent state when the orientation changes.

Since the return argument is a single Object and I want to return two items I’ve come up with the following solution:

@Override
public Object onRetainNonConfigurationInstance() {

    HashMap< String, Object> data = new HashMap<String, Object>();

    data.put( "mAdapter", getExpandableListAdapter() );
    data.put( "folderList", folderList );

    return data;

}

When I collect the data in my onCreate method with:

HashMap<String, Object> savedData = ( HashMap< String, Object> ) getLastNonConfigurationInstance();

I get an unchecked cast warning from the compiler. I assume this is because the compiler cannot determine if the HashMap with the specified types is actually going to be in the Object return by getLastNonConfigurationInstance. I then cast the Objects in the HashMap to the proper datatypes. My question is this:

Is this a safe way to pass multiple pieces of data back to the activity onCreate when I know that the data is going to be returned in the form of a HashMap (because I stored it there)?

I think I can suppress the warning with @SuppressWarnings(“unchecked”) but I want to be sure that my code is valid.

Kind regards,
Ivo

  • 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-20T08:50:48+00:00Added an answer on May 20, 2026 at 8:50 am

    Why use a hashmap instead of a simple struct-like holding class when you know the comprehensive set of contents at compile time?

    Also, beware passing anything that holds a reference to a Context/Activity between activity instances like this. Chances are that adapter you’re passing holds a Context reference so that it can get a LayoutInflater to inflate item views. This will have two negative effects:

    • Activities can be big, and hanging on to a reference to one that has been destroyed will keep the garbage collector from collecting it. (This is the sort of thing people mean when they talk about leaking contexts or activities.)
    • Contexts carry configuration info like screen orientation and theme. If you reuse an old context for something like inflating layouts after a configuration change, you’ll end up using the wrong configuration data. If you’ve used the resource system to provide different layouts for portrait and landscape modes, for example, this won’t work properly.

    This is why the method is called “non-configuration instance.” It is only correct to return objects that are not influenced by configuration. In the case of something like an adapter that holds a context, pass the data that the adapter accesses rather than the adapter itself and create a new adapter in the new activity instance.

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

Sidebar

Related Questions

I want to use a AsynTask to parse XML data. So in the Activity
I want to use an AutoCompleteTextView in my activity and populate the data as
I want to use an activity as dialog and i made the theme of
I just want to ask what is efficient way of use activity. Mean use
i want to use Android spinner with following data. Spinner will display only text
I want to use adb push command in my activity so i can transfer
I want to use a listview with checkboxes and some other options with out
i want to send data that reside in the Activity file and extends service
i am using webview in activity and want to use option menu too. but
Activity where I want to use: import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; public class BankoTinklasMap extends

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.