3

I'm writing an application that establishes a session with a website (mantaining cookies in requests). The session context (an object that controls all the requests) is actually stored in a member of the application class (I need all activities and services can access to this object to retrieve data).

But now, my problem is the next:

When I exit from the aplication (minimizing or pressing the back button) my application dies, I mean, the process exits and the application is destroyed... so, when starts the application again, need to login... again....

My question is, its possible maintain my application on memory while the session is active?

I think, store the session cookies in SharedPreferences is not a good idea because I want an independant session context implementation from android.

Thanks.

5 Answers5

1

No, there is no guarantee that your application will stay in memory. This is by design.

The Android Operating system will automatically remove Apps from memory as it requires more resources.

Your best bet: Save the cookie information inside a file in the Apps data folder and read it when the App starts.

Using the save state will avoid losing data in case when Android closes your App (lack of resources, inactivity).

public class MyActivity extends Activity {
    public String cookieString;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set Layout
        setContentView(R.layout.main);

        if(savedInstanceState!=null) {
            // App has been closed by the OS before and is now being restored

            // read your data here from the bundle
            String cookieData =  savedInstanceState.getString("CookieData");
            if(!TextUtils.isEmpty(cookieData)) {
                cookieString = cookieData;
            }
        } else {
            // App is newly started or has been closed by user (i.e. via finish())

            // Your code to read the cookie saved in a text file. 
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        String cookieData =  savedInstanceState.getString("CookieData");
        if(!TextUtils.isEmpty(cookieData)) {
            cookieString = cookieData;
        }
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // Save the critical data
        outState.putString("CookieData", cookieString);
        super.onSaveInstanceState(outState);
    }
}

It will allow you to save the cookie data when the App gets paused or minimized and restore it when the App get restored too. Additionally when the user Logs in you should write the cookie Data to a file too and read when the Bundle is empty and if the file exists.

Tseng
  • 61,549
  • 15
  • 193
  • 205
1

You do not have to add any extra code to achieve this just mention

  android:persistent="true"

In your your manifest, that should do the job. Below is a sample Application manifest which I tested

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat"
        android:persistent="true"
        tools:ignore="AllowBackup,GoogleAppIndexingWarning">
        <activity
            android:name="com.yours.yoursonly.youractivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        </application>

If you want to know more in detail, I myself have answered in one of posts

Prevent activity from being destroyed as long as possible

I hope this helps

Community
  • 1
  • 1
Aravind.HU
  • 9,194
  • 5
  • 38
  • 50
0

You can not prevent Android from destroying your application when it needs to reclaim resources.

There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish(). The system may also destroy your activity if it's currently stopped and hasn't been used in a long time or the foreground activity requires more resources so the system must shut down background processes to recover memory.

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

TronicZomB
  • 8,667
  • 7
  • 35
  • 50
0

the android eco system has a rule that each app will die in certain cases, while one of them is running in the background while another app run on the foreground.

however, you could have a much lower chance of being killed if the app would continue its work using a foreground service.

here's some more information about processes on android.

the downside of using a foreground service is that the user would not understand why your app needs it, since what he did is to close the app, and now he sees a notification of the app, so you should think of a good excuse why it needs to keep running .

you can also avoid directly closing the app upon pressing on the back button, by calling moveTaskToBack when onBackPressed is called. it doesn't force not killing your app, but might take a little longer to kill it.

i think the best thing is to handle restoration of your app, by saving data after login has finished, but it depends on your needs.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

Please check whether Don't keep activities option is unchecked in Settings-->Developer Options--> on the right side panel you can find check box named: Don't keep activities

user2330792
  • 67
  • 1
  • 9