好的...我终于找到了解决这个问题的方法。最后,继承应用程序是正确的选择。我正在保存最后一个暂停的活动,如果同一活动随时恢复,我会显示对话框。我还必须处理配置更改,这会导致活动重新启动。如果有人遇到和我一样的问题,代码如下:
package at.***.**********.util;
import org.holoeverywhere.app.Application;
import android.app.Activity;
import android.content.ComponentCallbacks;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
public class MyCustomApplication extends Application {
private String lastActivity = "";
private Boolean configChanged = false;
public Boolean getConfigChanged() { return configChanged; }
public void setConfigChanged(Boolean configChanged) { this.configChanged = configChanged; }
private ActivityLifecycleCallbacks activityCallback = new ActivityLifecycleCallbacks() {
@Override
public void onActivityStopped(Activity activity) { }
@Override
public void onActivityStarted(Activity activity) { }
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) { }
@Override
public void onActivityDestroyed(Activity activity) { }
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { }
@Override
public void onActivityResumed(Activity activity) {
if (activity.getClass().getName().equals(lastActivity) && !getConfigChanged()) {
Log.v(activity.getClass().getName(), "Make activity show password dialog");
}
setConfigChanged(false);
}
@Override
public void onActivityPaused(Activity activity) {
lastActivity = activity.getClass().getName();
}
};
private ComponentCallbacks componentCallback = new ComponentCallbacks() {
@Override
public void onLowMemory() { }
@Override
public void onConfigurationChanged(Configuration newConfig) {
setConfigChanged(true);
}
};
@Override
public void onCreate() {
registerActivityLifecycleCallbacks(activityCallback);
registerComponentCallbacks(componentCallback);
Log.v("Application startet", "Application startet");
super.onCreate();
}
@Override
public void onTerminate() {
unregisterActivityLifecycleCallbacks(activityCallback);
unregisterComponentCallbacks(componentCallback);
super.onTerminate();
}
}
如果您不得不重新开始您的活动,您可以致电:
((MyCustomApplication)getApplication).setconfigChanged(true);
不要忘记,告诉 Android 在清单中使用您的自定义应用程序:
<application
android:name="at.***.**********.util.MyCustomApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:hardwareAccelerated="true" >
我确信可能有更好的方法,但我还没有找到。非常感谢你们!