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

  • Home
  • SEARCH
  • 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 8352003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:49:36+00:00 2026-06-09T08:49:36+00:00

I am adding TextViews to my layout and I want to keep them when

  • 0

I am adding TextViews to my layout and I want to keep them when screen orientation changes.

I tried the following code, but it’s not working.

public class DetailsActivity extends Activity {

    private HashMap<View, TextView> views = new HashMap<View, TextView>();;

    @Override
    public Object onRetainNonConfigurationInstance() {      
        return views;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.layout44);

        findViewById(R.id.layout44_11).setOnLongClickListener(onLongClickListener);
        ...

        HashMap<View, TextView> data = (HashMap<View, TextView>)this.getLastNonConfigurationInstance();

        if (data == null) {
            System.out.println("is null");
        } else {
            for(View key : data.keySet()) {
                ViewGroup parent = (ViewGroup) key;
                System.out.println(key + " " + data.get(key));

                System.out.println(parent.getChildCount());
                System.out.println("child: " + parent.getChildAt(0));;

                parent.removeView(data.get(key));
                data.get(key).setText("asd");
                data.get(key).setVisibility(View.VISIBLE);
                parent.addView(data.get(key));
            }
            System.out.println("-------------------");
        }
    }

    private View.OnLongClickListener onLongClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            ViewGroup parent = (ViewGroup) v;
            if(parent != null) {
                TextView tv = new TextView(getApplicationContext());
                tv.setText("TextView");
                views.put(parent, tv);
                parent.addView(tv);
            }
            return false;
        }
    };
}

I’m getting exception:

Caused by: java.lang.IllegalStateException: The specified child
already has a parent. You must call removeView() on the child’s parent
first.

Why? What am I doing wrong? How to solve it?

  • 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-09T08:49:38+00:00Added an answer on June 9, 2026 at 8:49 am

    When you change screen orientation, what you are in effect doing is destroying your Activity and then starting it again freshly.

    If you have added 10 TextViews in your onCreate() method, don’t worry, they will be added exactly as they were previously without any interaction.

    However, if your Activity has had some user input or some processing which has made the Activity stateful (The Activity has had button presses or data submitted) you will need to use onSavedInstanceState(Bundle out) to save all of the state information (Basic type, NOT Views) and then in onCreate() you can pull this information out of the Bundle to recreate the state.

    never pass Views around Activities (or between Orientation changes). Views are owned by their Activities, when they are passed around they bring their original Activities with them.

    If you are new to Android think of it like this:
    If you are in an Art Gallery and the building is about to be torn down and a new Art Gallery is going to be built (You switch from Landscape to Portrait) then you’ll want to take all of the art from the First Gallery and then put it in the new Gallery. You won’t rip the Walls (Views) out of the First Gallery to build the second Gallery, you’ll build the second Gallery and then hang the art onto the walls (Views) of the new Gallery.

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

Sidebar

Related Questions

I'm adding TextViews dynamically with code. Now I'm using a LinearLayout with horizontal orientation
I'm adding TextViews programmatically in a for-loop and add them to an ArrayList. How
Adding the row one by one in the button click.. (i tried but its
Currently I have tried aligning my Linear Layout to the left and center but
I have a fairly complex layout (containing RelativeLayouts, TextViews and ImageViews) that I want
I have the following code, see below. I want to add a View programmatically
I am adding to my layout a WebView to display justified text. I want
Adding more than one child to a WPF StatusBar results in poor layout with
Adding Exceptional Handling is a good practise, but I have a doubt, In our
Adding strings localization files in iPhone bundle is very simple and perfectly fine but

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.