Issue
I want to know, probably in onCreate() of my activity, if the app was previously closed as per the normal home/back button or if the user actually went into the Android settings and selected force close.
Is it possible to distinguish these two cases?
EDIT:
I think I might not have explained what I mean well enough. I've read the activity lifecycle document and I understand that fairly well.
Basically, I just want to know when the activity is created, whether or not the user has previously gone into the Android settings and hit force stop. If the app was force stopped, I want to take an action (specifically, display the splash screen on startup).
Lots of the answers below say that I can put a flag in onStop() or onDestroy(), and that these methods won't be called if the user hits force stop. The problem is that these methods would have ALREADY been called before getting to that point, because of this sequence:
- app is in the active state, being used
- User hits back button (onStop(), onDestroy() called), or home button (onStop() called), or recent apps button (onStop() called)
- user goes into android settings, taps force stop
In that situation, I will have put the flag into shared preferences on onStop(), but then the user hits force stop and the flag is still active in onCreate().
I don't want to display the splash screen unless the user has hit force stop in settings. I know this is not the way it should be done... but this decision was not made by me.
Solution
I ran into same issue yesterday and this is what I found stated by Mark Murphy. But I have managed to crack it.
Actually,whenever "Force Stop" is pressed,the system will clear all the memory occupied by the application except 2 things.
1:the shared preference
and 2:sqlite database
.
So I have done the trick by storing a boolean variable in application,manipulating the value and then check it on each start up of application.and it's done.
CONSTANTS.java
package com.mehuljoisar.forcestopdemo;
public class CONSTANTS {
//the changes made to value of below variable will be cleared on force stop,so whenever force stop occurs,the value of variable will be "isForceStopped=true" again.
public static boolean isForceStopped = true;
}
MainActivity.java
package com.mehuljoisar.forcestopdemo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
private Context mContext;
private Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
if(CONSTANTS.isForceStopped)
{
//this block of code will be executed in 2 scenario,
//1: when application is started for very first time
//2: when application is started after force stopping
Toast.makeText(mContext, "Display Splash-screen and move to next screen", Toast.LENGTH_SHORT).show();
//don't forget this part,it's important to make change so that next time splash screen can be avoided
CONSTANTS.isForceStopped=false;
//and then launch next screen
launchSecondScreen();
}
else
{
//directly launch next screen
launchSecondScreen();
}
}
private void launchSecondScreen() {
i.setClass(mContext, SecondActivity.class);
startActivity(i);
finish();
}
private void initialize() {
mContext = this;
i = new Intent();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
SecondActivity.java
package com.mehuljoisar.forcestopdemo;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mehuljoisar.forcestopdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mehuljoisar.forcestopdemo.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="com.mehuljoisar.forcestopdemo.SecondActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I hope it will be helpful !!
Answered By - Mehul Joisar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.