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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:42:21+00:00 2026-05-27T07:42:21+00:00

In CommonsWare eBook Android v3.6 pg 270, he discusses handling threads with rotation. He

  • 0

In CommonsWare eBook Android v3.6 pg 270, he discusses handling threads with rotation. He offers a solution where you create an inner static class to hold your state, call detach in the workflow and then attach again during screen rotation.

The problem is that each rotation will destroy the Activity and recreate it in Android, thus when you come back your thread may reference the destroyed activity giving you an exception for accessing an object set for collection.

I tried this in Mono Android and was unable to get it to work, I got an exception every single time. My question, hopefully Jonathan Pryer reads this is, how can I make this work in Mono Android? I have asked this twice on the forums with no results. So I am taking it to StackOverflow. I wanted to post the actualy code but I didn’t want to violate CommonsWare licensing. So please take a look at the example in the book.

  • 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-27T07:42:22+00:00Added an answer on May 27, 2026 at 7:42 am

    What was the exception? What’s the adb logcat output?

    The C# equivalent is the-same-but-different from the Java source. For example, here’s the “rotation-aware” version of the default Mono for Android sample project:

    [Activity (Label = "Scratch.PreserveCount", MainLauncher = true)]
    public class Activity1 : Activity
    {
        CountInfo Info;
    
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
    
            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
    
            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button> (Resource.Id.myButton);
    
            button.Click += delegate {
                SetButtonCount (button, Info.Count++);
            };
    
            Info = (CountInfo) LastNonConfigurationInstance;
            if (Info == null) {
                Info = new CountInfo {
                    Count = 1,
                };
            } else {
                SetButtonCount (button, Info.Count);
            }
        }
    
        void SetButtonCount (Button button, int count)
        {
            button.Text = string.Format ("{0} clicks!", Info.Count);
        }
    
        public override Java.Lang.Object OnRetainNonConfigurationInstance ()
        {
            return Info;
        }
    
        class CountInfo : Java.Lang.Object {
            public int Count;
        }
    }
    

    It’s the same basic approach as the Java sample: the Activity.OnRetainNonConfigurationInstance() method is invoked by Android before the Activity is disposed, so we return our “state” from that method. The Activity.LastNonConfigurationInstance property will return null the first time it’s invoked, otherwise it will return the last value returned from OnRetainNonConfigurationInstance(). The only missing piece of the puzzle is that the “state” object (CountInfo here) must inherit from Java.Lang.Object as we’re passing the instance to Java.

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

Sidebar

Related Questions

package com.commonsware.android.threads; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener;
package com.commonsware.android.layouts; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import java.util.Date; public class
So I am reading Commonsware's Android Programming Tutorials and I am stuck with the
So I just started learning Android and I am reading the commonsware android development
Here is my code: package com.commonsware.android.skeleton; import android.app.Activity; import android.content.Context; import android.hardware.Camera; import android.hardware.Camera.*;
I try to add the commonsware TouchListView library ( https://github.com/commonsguy/cwac-touchlist ) to my android
package com.commonsware.cwac.wakeful.demo; import android.app.ListActivity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.provider.BaseColumns; import android.util.Log;
According to this CommonsWare example I managed to get my RelativeLayout subclass to be
I am using the following code to add multiple TextViews to CommonsWare ViewSwiper but
I need help explaing the following example below (c) CommonsWare. I know it makes

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.