我有一个应用程序,每次用户访问应用程序时我都必须显示启动画面。这就是他是重新启动应用程序,还是他推出了应用程序,或者他是否点击了启动器图标。
问题是在启动画面之后我还想回到他离开应用程序时可见的最后一个活动(如果它不是一个新的开始)
为了解决这个问题,我扩展了Application并实现了ActivityLifecycleCallbacks。
所以,当我的应用程序出现在bacground并回到foregroud时,我会从
启动Splash屏幕@Override
public void onActivityStarted(Activity activity) {
//launch splash screen
}
现在,一旦启动屏幕工作完成,我就会将其杀死,并且当用户上次使用应用程序之前我回到了应用程序进入后台之前的任何活动。
这里似乎工作正常,但我一直收到以下错误:
09-14 16:44:46.746: E/ActivityThread(5622): Performing pause of activity that is not resumed: {SignInActivity}
09-14 16:44:46.746: E/ActivityThread(5622): java.lang.RuntimeException: Performing pause of activity that is not resumed: {SignInActivity}
09-14 16:44:46.746: E/ActivityThread(5622): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3015)
09-14 16:44:46.746: E/ActivityThread(5622): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3003)
09-14 16:44:46.746: E/ActivityThread(5622): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2981)
09-14 16:44:46.746: E/ActivityThread(5622): at android.app.ActivityThread.access$1000(ActivityThread.java:135)
09-14 16:44:46.746: E/ActivityThread(5622): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
09-14 16:44:46.746: E/ActivityThread(5622): at android.os.Handler.dispatchMessage(Handler.java:102)
09-14 16:44:46.746: E/ActivityThread(5622): at android.os.Looper.loop(Looper.java:136)
09-14 16:44:46.746: E/ActivityThread(5622): at android.app.ActivityThread.main(ActivityThread.java:5001)
09-14 16:44:46.746: E/ActivityThread(5622): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 16:44:46.746: E/ActivityThread(5622): at java.lang.reflect.Method.invoke(Method.java:515)
09-14 16:44:46.746: E/ActivityThread(5622): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-14 16:44:46.746: E/ActivityThread(5622): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-14 16:44:46.746: E/ActivityThread(5622): at dalvik.system.NativeStart.main(Native Method)
所以我有两个问题:
谢谢, 晴天
答案 0 :(得分:0)
在Oncreate方法中,您可以启动ActivityForResult
startActivityForResult(intent, activity);
然后按照您的活动:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
答案 1 :(得分:0)
如果我理解正确,即使在应用程序被完全销毁/终止后,您仍希望显示上一个活动。您需要持久性数据来实现我的想法。我使用下面的共享首选项作为示例(尚未测试它,但我希望您能得到这个想法)。
//Inside every activity of your app
@Override
public void onResume(){
super.onResume();
//Store the current activity name inside shared preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("LastActivity", getCurrentActivityName());
editor.commit();
//Show splash screen here every time the app is brought to foreground (aka it resumes)
}
private void openActivityFromName(String name){
Class<?> c = null;
if(name != null) {
try {
c = Class.forName(name);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Intent intent = new Intent(this, c);
startActivity(intent);
}
private String getCurrentActivityName(){
this.getClass().getSimpleName()
}
/**
* Inside your main activity (the one that opens first normally)
*/
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String lastActivity = settings.getString("LastActivity", "");
//Open the last opened activity everytime the app opens
openActivityFromName(lastActivity);
}
将您的应用带到后台(不关闭它)不会关闭您当前的活动。因此,只有在应用程序被完全销毁并再次调用onCreate
后才需要打开上一个活动的行为。
答案 2 :(得分:0)
我遇到了同样的问题。我认为你应该只使用onResume方法并最终使用onPause()方法来控制你的活动状态。
放上onResume()方法
@Override
protected void onResume()
{
super.onResume();
...//
}