我昨天遇到同样的问题,this是我发现由马克墨菲说的。 但我设法破解它。
实际上,无论何时按下“强制停止”,系统都会清除应用程序占用的所有内存,除了2件事情。
1:the shared preference
和2:sqlite database
。
所以我通过在应用程序中存储一个布尔变量来完成这个技巧,操作这个值,然后在应用程序的每次启动时检查它并完成它。
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>
我希望这会有所帮助!
总是在onCreate()中显示启动画面。 onCreate()仅在应用程序完成时调用。点击“返回”按钮不会调用onDestroy()。退出到主屏幕,然后重新启动应用程序将调用onResume(),而不是onCreate()。我原来的答案是。你误解了生命周期。 – 2012-02-22 20:06:50
事实是,当用户按下后退按钮时,总是调用onDestroy()。尝试一下!按回来,然后重新启动应用程序将导致onCreate()被调用。 – howettl 2012-02-22 20:54:58
此外,onDestroy()/ onCreate()也会在设备方向更改时调用。我绝对不希望我的启动画面在任何时候改变方向。 – howettl 2012-02-22 21:02:56