我昨天遇到同樣的問題,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