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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:39:03+00:00 2026-05-25T14:39:03+00:00

I’m writing a clock app that uses some animation logic. In onCreate(), my app

  • 0

I’m writing a clock app that uses some animation logic. In onCreate(), my app Activity allocates an instance of a class that extends Thread which performs the logic processing in a loop in run(). I’m using a threaded SurfaceView (as per the LunarLander example, but with the context fix switch) to manage drawing to a Canvas.

The app runs fine as long as it has the focus. I suspend() the logic thread in onPause(), and resume() it in onResume() and stop() it in onDestroy() to make sure it’s not consuming resources when the user switches away.

This app runs without issues on the emulator. I can switch away and back to the app as often as I want with no ill effects.

But when running under Android on physical hardware, if you switch away from my app and back a couple of times, the entire system will become unresponsive. Eventually the phone ignores all input including the sleep/power switch on the top, and must be rebooted by removing the battery.

As a test, I disabled creation of the logic thread but left the rendering code all the same and running a small bit of animation logic, and it fixes the problem, so I feel like it is something I’m doing with this logic processing Thread that must be Very Bad for the system. I’m new to this, so I’m probably making a noob mistake.

Any help or pointers to info on how I could possibly profile this problem is appreciated.

EDIT : Listing the Activity source. I’ve taken out a small amount of cruft unrelated to the thread in hopes of clarifying the listing. (sorry if I messed up the formatting, new here)

public class EyesClockActivity extends Activity
implements SensorEventListener
{
// ---- options (shared preferences) ----
// snip, couple of booleans 

// --------------------------------------------------------
private void    LoadPreferences()
{
}   // end EyesClockActivity.LoadPreferences()

// --------------------------------------------------------
private void    SavePreferences()
{
}   // end EyesClockActivity.SavePreferences()

// ========================================================
private class EyesClockActivityThread extends Thread
{
    private long m_LastUpdateMilliSeconds;

    private boolean m_running = false;

    public void SetRunning( final boolean running ) 
    {
        // if restarting, don't want huge time leap
        if (        ( m_running == false )
                &&  ( running == true ) )
        {
            m_LastUpdateMilliSeconds = System.currentTimeMillis();
        }

        m_running = running; 
    }

    // ----------------------------------------------
    public EyesClockActivityThread()
    {
        m_digits = new EyeDigit[4];
        int index;
        for ( index = 0; index < m_digits.length; ++index )
        {
            m_digits[ index ] = new EyeDigit();
        }

        m_digits[0].SetCurrentDigit( 0 );
        m_digits[1].SetCurrentDigit( 0 );
        m_digits[2].SetCurrentDigit( 0 );
        m_digits[3].SetCurrentDigit( 0 );

        m_TestDigit.SetCurrentDigit( m_currentDigit );

        m_LastUpdateMilliSeconds = System.currentTimeMillis();

        m_lastDigitChangeMilliseconds = System.currentTimeMillis();

        int minute = Calendar.getInstance().get(Calendar.MINUTE);
        int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY );
        SetTime( hour, minute );            
    }   // end EyesClockActivityThread constructor

    // --------------------------------------------------------------------
    // sets digits to display specified hour,minute
    private void    SetTime( final int hour, final int minute )
    {
    }   // end method EyesClockActivityThread.SetTime()

    // ----------------------------------------------
    public  void    run()
    {
        while (true)
        {
            if ( m_running )
            {
                final   float   updatesPerSecond = 30.0f;

                final   long    milliSecondsBetweenUpdates = (long)(( 1.0f / updatesPerSecond ) * 1000.0f);

                long    currentTimeMilliSeconds = System.currentTimeMillis();

                // TODO sleep here instead of checking constantly
                if ( (currentTimeMilliSeconds - m_LastUpdateMilliSeconds) > milliSecondsBetweenUpdates )
                {
                    int minute = Calendar.getInstance().get(Calendar.MINUTE);

                    // don't bother setting time unless minute has changed
                    if ( (minute % 10) != m_digits[0].GetCurrentDigit() )
                    {
                        int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY );

                        SetTime( hour, minute );
                    }

                    float   secondsSinceLastUpdate = (float)((currentTimeMilliSeconds - m_LastUpdateMilliSeconds) / 1000.0);

                    for ( EyeDigit digit : m_digits )
                    {
                        digit.Update( secondsSinceLastUpdate );
                    }

                    // global update routines
                    m_LastUpdateMilliSeconds = System.currentTimeMillis();
                }
            }   // end if m_running
        }
    }   // end EyesClockActivityThread.run()

}   // end class EyesClockActivityThread

private EyesClockActivityThread     m_EyesActivityThread;

private EyesClockSurfaceView        m_EyesSurfaceView;

// ---- sensor interface ----
private SensorManager m_SensorManager;
private Sensor m_Accelerometer;


// -------------------------------------------------------------------------------
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    EyeDigit.InitDigitDescriptors();

    m_EyesActivityThread = new EyesClockActivityThread();

    // No Title bar
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    m_EyesActivityThread.SetRunning(true);
    m_EyesActivityThread.start();

    LoadPreferences();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    m_EyesSurfaceView = (EyesClockSurfaceView)findViewById(R.id.eyesclocksurfaceview);

    Configuration config = getResources().getConfiguration();

    m_EyesSurfaceView.SetOrientation( config.orientation );

    m_EyesSurfaceView.SetDigits( m_EyesActivityThread.GetDigits() );

    m_SensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    m_Accelerometer = m_SensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}   // end method EyesClockActivity.onCreate()

// ----------------------------------------------
protected void onPause() 
{
    super.onPause();

    // pause activity thread
    m_EyesActivityThread.SetRunning(false);
    m_EyesActivityThread.suspend();

    // quit sensor listening
    m_SensorManager.unregisterListener(this);
}   // end method EyesClockActivity.onPause()

// ----------------------------------------------------
protected void onResume() 
{
    super.onResume();

    // resume activity thread
    m_EyesActivityThread.SetRunning(true);
    m_EyesActivityThread.resume();

    m_SensorManager.registerListener(this, m_Accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}   // end method EyesClockActivity.onResume()

// ------------------------------------------------------
protected   void    onDestroy()
{
    super.onDestroy();

    m_EyesActivityThread.SetRunning(false);
    m_EyesActivityThread.stop();

    m_SensorManager.unregisterListener(this);

    // quit sensor listening
    m_SensorManager.unregisterListener(this);
}   // end method EyesClockActivity.onDestroy()

// -------------------------------------------------------
public void onAccuracyChanged(Sensor sensor, int accuracy) 
{
}

// ----------------------------------------------------
public void onSensorChanged(SensorEvent event) 
{
    if (        ( event.values[0] > 15.0f )
            ||  ( event.values[1] > 15.0f )
            ||  ( event.values[2] > 15.0f ) )
    {
        m_EyesActivityThread.StartGoogleyEvent();
    }
}   // end method EyesClockActivity.onSensorChanged()

// ---- options menu ----
@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // add cursor blink toggle item
    menu.add( Menu.NONE, R.id.toggle_blink, Menu.NONE, R.string.string_blink_cursor);

    if ( GetTwelveHourDisplay())
    {
        menu.add( Menu.NONE, R.id.toggle_twelve_hour_display, Menu.NONE, R.string.string_24_hour_display );
    }
    else
    {
        menu.add( Menu.NONE, R.id.toggle_twelve_hour_display, Menu.NONE, R.string.string_12_hour_display );
    }

    return true;
}    

@Override
public boolean onPrepareOptionsMenu(Menu menu) 
{
    super.onPrepareOptionsMenu(menu);

    MenuItem displayItem = menu.getItem(1);

    if ( displayItem != null )
    {
        if ( GetTwelveHourDisplay())
        {
            displayItem.setTitle(R.string.string_24_hour_display);
        }
        else
        {
            displayItem.setTitle(R.string.string_12_hour_display);
        }
    }

    return true;
}    

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    // Handle item selection
    switch (item.getItemId()) 
    {
    case R.id.toggle_twelve_hour_display:
        ToggleTwelveHourDisplay();

        int minute = Calendar.getInstance().get(Calendar.MINUTE);
        int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY );
        m_EyesActivityThread.SetTime( hour, minute );

        SavePreferences();

        return true;
    case R.id.toggle_blink :
        ToggleBlinkCursor();

        SavePreferences();

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
} 

}
  • 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-25T14:39:04+00:00Added an answer on May 25, 2026 at 2:39 pm

    In java threads, stop(), resume(), and suspend() are deprecated and should not be used. The natural order for threads are to let it die in onPause() and restart a new one in onResume()

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I need a function that will clean a strings' special characters. I do NOT

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.