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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:55:56+00:00 2026-06-05T23:55:56+00:00

I have an activity in whose onCreate method an Init function is called (the

  • 0

I have an activity in whose onCreate method an Init function is called (the function calls some native code involving lot of stuffs and calls to the openSLES audio api). The point is that this Init function makes the app crash when called again, which happens on a screen rotation or when i close the activity using Back button and i launch it again (but if in the meanwhile the process is killed, i have no troubles). I can’t change the beaviour of the Init function.

I see that the process isn’t killed when the activity is destroyed, I expected this after reading the docs, and it’s a good thing since – if there is some audio signal playing – that continues playing after the activity has been destroyed, which is good for my purposes.

I tried to perform a check on the initialization state using onSaveInstanceState, but that works well only on screen-rotation, that’s when onSaveInstanceState is called. The callback is not called when i push the Back button.

So i tried to use Shared Preferences, performing the state saving in onPause. But at this point i have the opposite problem: if the process is killed, the Shared Preferences values are kept, but in that case i need to perform Init again for the app to work properly.

I guess i need a way to know for sure if my activity is created after a process kill or not, but at the moment i can’t see how. I thought about using the bundle instance in onPause method, but i can’t figure how and whether this is possible. Any kind of hint would be really appreciated.

  • 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-05T23:55:58+00:00Added an answer on June 5, 2026 at 11:55 pm

    There’s a simple solution to this problem. You don’t need to save things in SharedPreferences to accomplish this. Just use a static (class) variable. Like this:

    public class Globals {
        public static boolean initialized = false;
    }
    

    The variable initialized will be set to false when the class is loaded. Only once. In your code, you then check and set the variable like this:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Initialize (but only once per process)
        if (!Globals.initialized) {
            init(); // Call init function that does things one time per process
            Globals.initialized = true; // Remember we are initialized so we don't
                                        //  do it again
        }
        ...
    }
    

    Even if all your activities are finished, if the OS doesn’t kill your process the variable initialized will still be “true” if the application is started again. Once the OS kills the process, the variable will be set to “false” the next time the application is started and a new process is created.

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

Sidebar

Related Questions

I have Activity with ListView inside it and in the onCreate method of the
I have an activity in whose oncreate event I start a custom view embedded
I have a simple AsyncTask whose doInBackground() calls a function fn1 which in turn
I have an activity which shows some List entries. When I click on a
I have an activity that launches another activity with startActivityForResult method. I would like
I have an activity that runs some ASCII control over a network port to
This is my onCreate method of my first activity of my app. It initalizes
Hullo, I have an activity whose layout is made of a single FrameLayout. I
I have a problem: Java Code public class VisualizzaListaActivity extends TabActivity { /** Called
I have Activity A with android:launchMode=singleTop in the manifest. If I go to Activity

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.