我昨天遇到了同样的问题,这就是我发现马克墨菲所说的。但我设法破解了它。
实际上,每当按下“强制停止”时,系统都会清除应用程序占用的所有内存,除了两件事。
1:the shared preference
和2:sqlite database
。
所以我通过在应用程序中存储一个布尔变量来完成这个技巧,操纵该值,然后在每次启动应用程序时检查它。它完成了。
常量.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>
我希望它会有所帮助!