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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:39:58+00:00 2026-06-13T03:39:58+00:00

I need to test my apps lifecycle and its destruction and re-creation, because when

  • 0

I need to test my apps lifecycle and its destruction and re-creation, because when the keyboard slides in/out (or is rotated), the app is destroyed and re-created. I also need to test for memory leaks when it does this.

I saw in the tutorial http://developer.android.com/tools/testing/activity_test.html#StateManagementTests

Terminate the activity and restart it:
mActivity.finish();
mActivity = this.getActivity();

However this is very simplified. My logging seems to indicate that calling finish() spawns a background thread with a different thread id which then calls onPause(), finish(), onStop(), and onDestroy(). I even tried getInstrumentation().waitForIdleSync(); to try to wait for that background thread to complete, but I still get race conditions when I test for expected values.

Not only that, but when it re-creates the Activity (when the 1st race condition doesn’t occur or I comment out the assert), with getActivity(), then it just returns the exact same object that I just finished! I can tell because I log this in my onXXX…() methods.

This differs from the orientation rotation / keyboard sliding app lifecycle, which always creates a new Activity object.

So how can I test this destruction / reincarnation scenario?

  • 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-13T03:40:00+00:00Added an answer on June 13, 2026 at 3:40 am

    Ok I found it! It involves LOTs of waitForIdleSync() since it is multi-threaded to begin with, and setActivity(null). I could only get within +- 30% of the same memory. I would like to give credit to Peter Carpenter too, because his idea sped up my app and I would never have thought of that!

    public void testMemoryLeaks() {
        Log.e(TAG, "------------ testMemoryLeaks()");
        getInstrumentation().waitForIdleSync();
        System.gc();
        Main mActivity = getActivity();
        Log.d(TAG, "-- Extractor.stop() "+mActivity.getExtractor());
        mActivity.getExtractor().stop();
        getInstrumentation().waitForIdleSync();
        System.gc();
        long mem = Runtime.getRuntime().freeMemory();
        Log.d(TAG, "-- freeMemory: " + mem);
        Log.d(TAG, "-- mActivity.finish()");
        mActivity.finish();
        getInstrumentation().waitForIdleSync();
        Log.d(TAG, "-- setActivity()");
        setActivity(null);
        getInstrumentation().waitForIdleSync();
        System.gc();
        Log.d(TAG, "-- getActivity()");
        mActivity = getActivity();
        assertTrue(mActivity != null);
        Log.d(TAG, "-- Extractor.stop() "+mActivity.getExtractor());
        mActivity.getExtractor().stop();
        getInstrumentation().waitForIdleSync();
        System.gc();
        long memAfter = Runtime.getRuntime().freeMemory();
        Log.d(TAG, "-- freeMemory: " + memAfter);
        assertTrue("Memory leak", mem > memAfter * .70 && mem < memAfter * 1.30);
        mActivity.finish();
        getInstrumentation().waitForIdleSync();
        Log.d(TAG, "-- end testMemoryLeaks()");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to test my application. Its a VoIP but each time I need
I need to test my Android app for playing flash objects in the WebView.
I need to test Android App specifically on LG Ally mobile. I am on
I need to be able to test 27 different web apps in isolation, so
I need to write a simple WinForms apps that can be fired to test
To test apps that use internet connections i need something to configure the quality
I need my Android app to save it's state to disk when its activity
I need to test Perl Application ( File operation , Data base operation..etc )
I need to test if a user can write to a folder before actually
I need to test the bandwidth I have on my USB RNDIS connection. I

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.