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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:24:41+00:00 2026-06-18T09:24:41+00:00

The following is a stripped down version of a Fragment I’m using to display

  • 0

The following is a stripped down version of a Fragment I’m using to display a simple stopwatch. The app is working perfectly on a tablet. However, on a phone, where an orientation change will cause the fragment to be re-created in a new activity, I’m getting a memory leak.

public class TimerFragment extends BaseFragment {

    private static final int MESSAGE_UPDATE_TEXT = 0;

    private TextView text;

    private static final String KEY_START_TIME = "KEY_START_TIME";
    private static final String KEY_ELAPSED_TIME = "KEY_ELAPSED";

    private long startTime;
    private long elapsedTime;

    private static boolean running;

    private final TimerHandler handler = new TimerHandler(this);

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     
        setHasOptionsMenu(true);
        setUpHomeButton(false);

        View view = inflater.inflate(R.layout.fragment_timer, container, false);

        text = (TextView) view.findViewById(R.id.timerText);

        if(savedInstanceState != null) {
            startTime = savedInstanceState.getLong(KEY_START_TIME);
            elapsedTime = savedInstanceState.getLong(KEY_ELAPSED_TIME);
        }

        if(running) {
            handler.removeCallbacks(timer);
            handler.postDelayed(timer, 0);
        }

        updateText();

        return view;
    }

    private void updateText() {
        if(elapsedTime == 0L) {
            text.setText("0:00");
            return;
        }

        int sec = (int) (elapsedTime / 1000);
        int min = sec / 60;
        sec = sec % 60;

        text.setText(min + ":" + sec);

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        handler.removeCallbacks(timer);

        outState.putLong(KEY_START_TIME, startTime);
        outState.putLong(KEY_ELAPSED_TIME, elapsedTime);
    }

    private final Runnable timer = new Runnable() {

        @Override
        public void run() {
            long now = System.currentTimeMillis();
            elapsedTime = now - startTime;

            running = true;
            handler.postDelayed(this, 1000);
            handler.sendEmptyMessage(MESSAGE_UPDATE_TEXT);
        }
    };

    private static class TimerHandler extends Handler {
        private final WeakReference<TimerFragment> weakFragment;

        public TimerHandler(TimerFragment f) {
            weakFragment = new WeakReference<TimerFragment>(f);
        }

        @Override
        public void handleMessage(Message msg) {
            TimerFragment fragment = weakFragment.get();
            switch (msg.what) {
            case MESSAGE_UPDATE_TEXT:
                if(fragment != null) {
                    fragment.updateText();
                }
                break;

            default:
                break;
            }

        }
    }
}

Eclipse MAT even gives me this:

One instance of "android.os.MessageQueue" loaded by "<system class loader>" occupies 1     529 600 (14,19%) bytes. The memory is accumulated in one instance of "android.os.Message"     loaded by "<system class loader>".

Keywords
android.os.Message
android.os.MessageQueue

However, the above does not appear when I analyze memory when running on a tablet (single activity at all times).

Any ideas for how to fix this? If I understand the problem correctly, which I might not, I think there are multiple handlers getting created when an orientation change happens on a phone.

If my problem is hard to understand give me a hint and I will try to explain better.

  • 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-18T09:24:42+00:00Added an answer on June 18, 2026 at 9:24 am

    There is a simple rule you should always follow when using Handler. Every time you write handler.postDelayed you should also write handler.removeCallbacks somewhere.

    In your case you are calling handler.postDelayed(timer, 0) in onCreateView method so onDestroyView is the most appropriate place to call handler.removeCallbacks(timer)

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

Sidebar

Related Questions

I have 2 tables. The following are just a stripped down version of these
I have the following sample code (a stripped down version of my programme) Class
I have the following code: http://pastebin.com/EgjbzqA2 which is basically just a stripped down version
I want to test the following logic (this is obviously a stripped-down version of
I observed some strange behavior regarding injecting EntityManager. Following is stripped down version of
I have the following (stripped down) code: <?PHP class A { function Show(){ echo
I have the following code (stripped down): public class Character extends MovieClip { var
I have the following JSF code (stripped down for the sake of sanity): <!DOCTYPE
I have a stripped down version of Java 1.4.2 that does not have the
The following Java code is a stripped down example of the code I need.

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.