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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:14:47+00:00 2026-06-16T23:14:47+00:00

I am trying to make an android application, which uses some jni code. This

  • 0

I am trying to make an android application, which uses some jni code.
This jni code allocates some memory, which has to be freed. Let`s
suppose I have this class containing this code:

class MyJniClass{

public native void jni_init(); //this allocates some memory
public native void do_something(); //this also allocates some memory,  
which is linked to the memory allocated in jni_init()
public native void jni_destroy(); //this frees all the allocated  
memory in two previous functions

... // some other java code

}

My trouble is, where in activity lifecycle to call jni_init and jni_destroy. So I started some digging how exactly the activity lifecycle works and I am a little bit confused about the results. I suppose that this is caused by the fact, that I am trying to
understand quite deeply to the level of linux processes. But I am using jni, so it makes some sense.

I created a simple activity, implemented all the lifecycle callbacks and put some logging into them. I did the same with the constructor and I also added jni code, which logs the pid of the process.

So, what I found
– the activity can be destroyed even if onDestroy is not called (this is clear, I found lots of explanations for this) and the process is still runnig (verified by apps like Usage Timelines, ps command and the getpid jni call). The process is running, so it seems logical to keep the memory allocated.

The problem is, where to allocate the memory. I cannnot use onCreate, because it can called several times during the process lifetime, so memory leaks can happen. I also don’t want to use onStart for allocation and onStop for freeing, because it unnecessary work to do. I tried the singleton design, it seems to work, but I don’t want to use it, because I need MyJniClass to be reentrant (I want to use it also in a service). So my question is – is there some recommended way how to do memory management in jni apps?

Edit:
I see I didn`t describe the most confusing part of this clearly enough. Here is the sequens of events:

1) I start the app using the icon in launcher

2) new activity is created (constructed)

3) onCreate is called

4) onStart is called

5) onResume is called

6) I press the home button

7) onSaveInstanceState is called

8) onPause is called

9) onStop is called

10) I start the app using the icon in launcher (as in step 1)

11) new activity is created (constructed)

12) onCreate is called

13) onStart is called

14) onResume is called

The confusing steps are 11) and 12) because the process (pid) is still the same, but as I understand from documentation, the previous process should be killed if 11) and 12) happen.

Thanks and sorry for all the grammar mistakes.

  • 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-16T23:14:48+00:00Added an answer on June 16, 2026 at 11:14 pm

    From here

    android:finishOnTaskLaunch

    Whether or not an existing instance of the activity should be shut down (finished) >whenever the user again launches its task (chooses the task on the home screen) — "true" if >it should be shut down, and "false" if not. The default value is "false".

    It clearly states that you should change this value in AndroidManifest.xml if you want your activity to exist only once.

    There are also other value you can change which will impact the activity life cycle behavior.

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

Sidebar

Related Questions

I am trying to make my Android application run some code when run from
I'm trying to make application for tablet with Android 2.3 with this kind of
I've been trying to make an application which has very rich Database mySQL with
I am trying to make android application which should take ms word files i.e.(
I'm trying to make an android application which displays the latest news off of
I am trying to make an android application in which if you drag image
I am trying to make an android application in which i would like to
I'm new to android. I'm trying to make an application in which the user
I am trying to make simple notepad application for learning android development. So, to
This Android application on Google uses the following method to refresh the database after

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.