VinothKathir
VinothKathir

Reputation: 200

Back-Key press behavior in Android & issues

We have an App containing Multiple Activities and Data Models, Controllers & bunch of core classes all tied up together.

ISSUE: When we click on Back-Key @ the homepage, only the HomePage’s activity is getting killed but the rest of the App remains silent in Background. This behaviour in Android looks strange. And when the App is re-launched, Not the whole is relaunched. Only the Homepage activity is relaunched and rest of the Models, controllers etc stay in memory. As many of them are implemented as Singleton objects.

The Mainpage actvitity contains the core workflow of Data calls that executes REST APIs building the models and triggering the controllers. If the whole of the App got killed on Back-Key press, we’d be better off. But since it’s killing only the mainpage, when App is relaunched , it’s forcing us to implement Session-management type of design. Which I feel could be avoided. What do you suggest here?

When the Activity is re-launched,

A. Should we check for Old data in BG and try to continue from there? OR ,

B. On detecting Data/controllers exising in Background, Should the App be forcefully re-set to 0th state and let it give a fresh start? OR,

C. Should we forcefully KILL the App on back-key press? That would save us from the need to maintain the sessions.

-----------------------------//Updated//----------------------------------------

Reply to "Demand"

Thanks for your reply. Yes, this is my first Android project. And I'm learning things from scratch. My Application does not have any Services / Broadcast receivers.
It's purely running just with Activities.

We do not have models as singletons. We do have a bunch of core classes, which needs to be singleton to maintain the instance across scopes. In fact many of them are now changed to fly-weight pattern. So I doubt if we are really doing something wrong there.

My key issue is, When you press back-key, it "kills" the home screen activity. How do I say it's "killing" the activity, - because, when I re-launch the App from App drawer, the control is flowing through "OnCreate()" of the activity. Which would trigger a data-fetch-model-update routines.

If the App doesn't flow through this "OnCreate()" on successive launches, We do not have any problem. Just because it does, we are forced to think about what if the data-fetch routine happens twice , should we ignore the second one or clear the old one .etc.

And the other info I would like to Share is, Pressing on the Home button pushes the App to BG. So we are spared from this issue. When we click on the App icon in App drawer, The App does NOT get relaunched so doesnt go through the Homescreen Activity's OnCreate() method, it swiftly brings the App to foreground. This happens only when you have pressed the Home button & trying to come back again.

Back-key press is the only thing that's causing the problem. I was thinking this is the standard behavior of Android Apps. So resuming the activity from "Stopped" state also makes the Activity go through OnCreate() ?

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.poc_viewpager"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:name=".ApplicationController"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ViewPagerActivity"
android:label="@string/title_activity_view_pager"
android:screenOrientation="portrait">
</activity>
</application>

</manifest>

Upvotes: 1

Views: 468

Answers (2)

David Wasser
David Wasser

Reputation: 95618

Seems there is some confusion here. You need to read a bit more about the whole concept of tasks, activities and activity stacks (there's lots of stuff on SO or in the Android documentation).

When you press the BACK key, the standard behaviour is to finish the current Activity. Basically "finishing" an Activity kills it. It is removed from the activity stack in the task, revealing any Activity that may be underneath it. Android will eventually call onDestroy() on the Activity and the object will be reclaimed by the garbage collector.

If you only have 1 Activity in your task, then finishing that Activity in essence "quits" your application. The task has no activities, so it will be removed from the system.

Starting your application again will create a new task, create a new instance of your Activity and will call onCreate() on it. That's all standard behaviour.

You wrote:

When we click on Back-Key @ the homepage, only the HomePage’s activity is getting killed but the rest of the App remains silent in Background.

I'm assuming that by HomePage's Activity you mean your MainActivity. Yes, it is killed (finished).

Your application is hosted in a OS process. This is created by Android when you launch your app. The OS process has a virtual machine and your code runs inside the virtual machine. Android manages and maintains its components (Activity, Service, BroadcastReceiver, Provider) inside the virtual machine. Android can kill the OS process at any time, if it needs the resources or if it knows that there are no active components running in the virtual machine. If you have singletons floating around in the virtual machine that aren't tied to an active component, then you have an inappropriate application architecture for Android. Android does not care about your singletons and it will kill the OS process when there are no active Android components in the virtual machine.

Another thing: If you run your App and then press the HOME key, your application (task) is pushed to the background. If you then launch your app again, Android will bring the task back to the foreground. This is what you are seeing. However, if you leave your application for too long in the background, Android will just kill it since it isn't being used and it is consuming valuable resources. Also, if you run another App that needs a lot of resources, Android will also kill your app since it is in the background. This can happen at any time and you have no control over it. So your app needs to be able to recover from all of these cases.

You can easily deal with this by having your MainActivity check, in onCreate() if your singletons are there. If they aren't, it can recreate them (as if the app was just started for the first time). If they are there, then it can skip that step.

Upvotes: 1

Alexander Mikhaylov
Alexander Mikhaylov

Reputation: 1800

So, I think you don't understand how android works. There are several core components in android application (Application, Activity, Service and etc). When you press back-button android are stopping (not killing) your activity (not application). This mean, that your application is still running (and services if you have, and broadcast receivers). Also if you start second screen from your home screen with sending intent, it looks like pressing back-button with home screen activity's point of view. I mean android will call onPause() -> onStop() callbacks in your home activity. This mean you can't kill your app here.

You don't know when exactly your application will killed by the system. So, technically you have problems with your architecture. So I don't know why you implementing model as Singleton (I think it's bad idea), so I can't suggest a solution.

Try to rethink your architecture. Hope it's help.

I suggest you to read this one

---Update--

So, with new information. You can trigger data-fetch-model-update routines not in your activity but in your application class. Application is creating when system start application (not exact activity) and stops when application is stopped by system. So your singletons will live until your application live. Move your code from your activity onCreate to your Application onCreate method. In this case your data fetch will start, when application start. I think it will solve your problem.

Look at this example

Upvotes: 1

Related Questions