Android – 执行停止没有恢复的活动

当我把我的应用程序推到后台,并做一些其他的东西,如WhatsApp或短信,onResume它的效果很好。
但我最近发现,当我打开/启动Facebook应用程序,而我的应用程序是在后台,我不知道发生了什么…
但onResume,该应用程序行为不端…
不要做什么是需要做的,但是当我回到主页,回来它工作正常请帮助我..如何解决它?

带有所有消息的Logcat(不带filter)

10-15 12:53:59.899: I/Adreno-EGL(32033): Remote Branch: quic/LNX.LA.3.5.1_RB1.1 10-15 12:53:59.899: I/Adreno-EGL(32033): Local Patches: NONE 10-15 12:53:59.899: I/Adreno-EGL(32033): Reconstruct Branch: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.02.048.018 + f2fd134 + NOTHING 10-15 12:53:59.924: D/OpenGLRenderer(32033): Enabling debug mode 0 10-15 12:54:00.000: V/AlarmManager(7677): sending alarm Alarm{42cfa490 type 3 android} 10-15 12:54:00.110: I/ActivityManager(7677): Displayed uk.org.humanfocus.hfi/.EvaluateTrainingActivity: +838ms 10-15 12:54:00.114: D/WifiStateMachine(7677): handleMessage: E msg.what=151572 10-15 12:54:00.114: D/WifiStateMachine(7677): processMsg: ConnectedState 10-15 12:54:00.114: D/WifiStateMachine(7677): processMsg: L2ConnectedState 10-15 12:54:02.258: V/AlarmManager(7677): sending alarm Alarm{42ebd600 type 1 com.facebook.katana} 10-15 12:54:02.274: V/AlarmManager(7677): sending alarm Alarm{42ec0ff0 type 1 com.android.chrome} 10-15 12:54:02.428: D/hardware_info(7386): hw_info_append_hw_type : device_name = speaker 10-15 12:54:03.011: W/BroadcastQueue(7677): Permission Denial: broadcasting Intent { act=android.net.conn.INET_CONDITION_ACTION flg=0x4000010 (has extras) } from null (pid=-1, uid=-1) requires com.facebook.permission.prod.FB_APP_COMMUNICATION due to registered receiver BroadcastFilter{41fdecd0 u0 ReceiverList{42b2f608 31941 com.facebook.katana/10103/u0 remote:429a17e8}} 10-15 12:54:03.011: W/BroadcastQueue(7677): Permission Denial: broadcasting Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 (has extras) } from null (pid=-1, uid=-1) requires com.facebook.permission.prod.FB_APP_COMMUNICATION due to registered receiver BroadcastFilter{41fdecd0 u0 ReceiverList{42b2f608 31941 com.facebook.katana/10103/u0 remote:429a17e8}} 10-15 12:54:03.118: D/WifiStateMachine(7677): handleMessage: E msg.what=151572 10-15 12:54:03.118: D/WifiStateMachine(7677): processMsg: ConnectedState 10-15 12:54:03.118: D/WifiStateMachine(7677): processMsg: L2ConnectedState 10-15 12:54:03.140: D/WifiStateMachine(7677): handleMessage: X 10-15 12:54:03.141: D/GCoreFlp(8174): Unknown pending intent to remove. 10-15 12:54:03.145: W/fb4a(:<default>):AbstractMqttPushService(31941): Attempt to start service that is already started 10-15 12:54:03.242: D/WifiStateMachine(7677): handleMessage: E msg.what=131155 10-15 12:54:03.242: D/WifiStateMachine(7677): processMsg: ConnectedState 10-15 12:54:03.243: D/WifiStateMachine(7677): processMsg: L2ConnectedState 10-15 12:54:03.245: D/WifiStateMachine(7677): handleMessage: X 10-15 12:54:03.319: D/dalvikvm(31941): GC_CONCURRENT freed 1833K, 9% free 20190K/22072K, paused 5ms+7ms, total 86ms 10-15 12:54:03.320: D/dalvikvm(31941): WAIT_FOR_CONCURRENT_GC blocked 68ms 10-15 12:54:03.323: W/MediaPlayer-JNI(31941): MediaPlayer finalized without being released 10-15 12:54:03.452: W/BroadcastQueue(7677): Permission Denial: broadcasting Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 (has extras) } from null (pid=-1, uid=-1) requires com.facebook.permission.prod.FB_APP_COMMUNICATION due to registered receiver BroadcastFilter{42b51d68 u0 ReceiverList{429feb50 31941 com.facebook.katana/10103/u0 remote:41fb8788}} 10-15 12:54:03.573: W/fb4a(:<default>):JACKSON_FALLBACK(31941): Using com.fasterxml.jackson.databind.deser.std.EnumDeserializer@42914bc8 to deserialize [simple type, class com.facebook.common.util.TriState] 10-15 12:54:03.587: W/fb4a(:<default>):JACKSON_FALLBACK(31941): Using com.fasterxml.jackson.databind.deser.std.EnumDeserializer@42bb3100 to deserialize [simple type, class com.facebook.contacts.graphql.contactprofiletype.ContactProfileType] 10-15 12:54:03.957: D/dalvikvm(31941): GC_CONCURRENT freed 3400K, 15% free 20455K/23952K, paused 4ms+7ms, total 88ms 10-15 12:54:03.957: D/dalvikvm(31941): WAIT_FOR_CONCURRENT_GC blocked 75ms 10-15 12:54:04.099: W/fb4a(:<default>):JACKSON_FALLBACK(31941): Using BeanSerializer for com.facebook.katana.newbookmark.qe.NewBookmarkConfig to serialize class com.facebook.katana.newbookmark.qe.NewBookmarkConfig 10-15 12:54:04.119: D/WifiStateMachine(7677): handleMessage: E msg.what=151572 10-15 12:54:04.120: D/WifiStateMachine(7677): processMsg: ConnectedState 10-15 12:54:04.120: D/WifiStateMachine(7677): processMsg: L2ConnectedState 10-15 12:54:04.124: D/WifiStateMachine(7677): handleMessage: X 10-15 12:54:04.177: W/fb4a(:<default>):JACKSON_FALLBACK(31941): Using com.fasterxml.jackson.databind.deser.std.EnumDeserializer@42a30980 to deserialize [simple type, class com.facebook.platform.webdialogs.PlatformWebViewActionManifest$FetchState] 10-15 12:54:04.197: I/dalvikvm(31941): Could not find method com.android.internal.widget.ILockSettings$Stub.a, referenced from method com.facebook.keyguardtype.LockSettingsServiceKeyguardTypeResolver.b 10-15 12:54:04.197: W/dalvikvm(31941): VFY: unable to resolve static method 5338: Lcom/android/internal/widget/ILockSettings$Stub;.a (Landroid/os/IBinder;)Lcom/android/internal/widget/ILockSettings; 10-15 12:54:04.197: D/dalvikvm(31941): VFY: replacing opcode 0x71 at 0x0023 10-15 12:54:04.440: I/SBar.NetworkController(7758): onSignalStrengthsChanged SignalStrength: 19 0 -120 -160 -120 -1 -1 99 2147483647 2147483647 2147483647 2147483647 2147483647 gsm|lte 0 -108 -1 false 5 5 0 0 0 99 99 99 5 level=5 10-15 12:54:04.814: V/WebViewChromiumFactoryProvider(31941): Binding Chromium to main looper Looper (main, tid 1) {41f8cbd0} 10-15 12:54:04.815: I/LibraryLoader(31941): Expected native library version number "",actual native library version number "" 10-15 12:54:04.816: I/chromium(31941): [INFO:library_loader_hooks.cc(116)] Chromium logging enabled: level = 0, default verbosity = 0 10-15 12:54:04.817: I/BrowserStartupController(31941): Initializing chromium process, renderers=0 10-15 12:54:04.822: E/AudioManagerAndroid(31941): BLUETOOTH permission is missing! 10-15 12:54:04.864: W/chromium(31941): [WARNING:proxy_service.cc(890)] PAC support disabled because there is no system implementation 10-15 12:54:05.121: D/WifiStateMachine(7677): handleMessage: E msg.what=151572 10-15 12:54:05.121: D/WifiStateMachine(7677): processMsg: ConnectedState 10-15 12:54:05.122: D/WifiStateMachine(7677): processMsg: L2ConnectedState 

这是onResume()

 super.onResume(); if (backgroundThreadRunning == true) { backgroundThreadRunning = false; } if (Constants.isVideoEditing) editingProgress.setVisibility(View.VISIBLE); else editingProgress.setVisibility(View.GONE); if (Constants.isAudioProcessing) addAudioProgress.setVisibility(View.VISIBLE); else addAudioProgress.setVisibility(View.GONE); if (isHomeKeyPressed() && !(isRecentActivity)) { isRecentActivity = false; homeKeyPressed(false); AlertDialog.Builder ab = new AlertDialog.Builder( CreateTrainingActivity.this); ab.setMessage( "Due to Other Application Launches, video process will be cancelled!\nAre you sure you want to cancel?") .setPositiveButton("Yes", dialogClickListener) .setNegativeButton("No", dialogClickListener).show(); } }; 

编辑:我如何修复这个问题

我在onResume()方法中编写了这段代码

 try { // check if any view exists on current view style = ((Button) findViewById(R.id.xyz_button)); } catch (Exception e) { // Button was not found // It means, your button doesn't exist on the "current" view // It was freed from the memory, therefore stop of activity was performed // In this case I restart my app Intent i = new Intent(); i.setClass(getApplicationContext(), MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); // Show toast to the user Toast.makeText(getApplicationContext(), "Data lost due to excess use of other apps", Toast.LENGTH_LONG).show(); } 

只要给我50美分的问题。 捕捉exception确实是一种可能性,但正确的方式来处理系统为其资源在后台死亡的活动问题是一个在Android的常见问题,根据谷歌的解决scheme是这样的:

onPause()是你处理用户离开你的活动的地方。 最重要的是, 在这一点上,用户所做的任何改变都应该被提交 (通常是持有数据的ContentProvider)。

重点是我的。 但是,这意味着Android生命周期的devise,使得在正常情况下onPause应该被称为ActivityFragment被发送到后台。 他们在几个android文档页面上暗示了这一点:

当您的活动进入暂停状态时,系统将在您的活动中调用onPause()方法,以允许您停止正在进行的暂停操作(例如video暂停)或保留任何应永久保存的信息用户继续离开你的应用程序。 如果用户从暂停状态返回到活动状态,系统将恢复并调用onResume()方法。

注意:当您的活动收到对onPause()的调用时,可能表明该活动将暂停一段时间,用户可能会将焦点返回到您的活动。 但是,这通常是用户离开活动的第一个迹象。

但最有可能帮助你的资源是这两个:

http://developer.android.com/training/basics/activity-lifecycle/stopping.html

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

你丢失的资源可能发生的事情是这样的:

当您的活动接收到对onStop()方法的调用时,它将不再可见,并且应该释放几乎所有在用户不使用时不需要的资源。 一旦您的活动停止,系统可能会销毁实例,如果它需要恢复系统内存。 …默认情况下,系统使用Bundle实例状态来保存活动布局中每个View对象的相关信息(如input到EditText对象中的文本值)。 所以,如果您的活动实例被销毁并重新创build,则布局的状态将恢复到之前的状态,而不需要任何代码。 但是,您的活动可能包含更多要恢复的状态信息,例如跟踪活动中用户进度的成员variables。

注意:为了使Android系统恢复活动中视图的状态,每个视图都必须有一个由android:id属性提供的唯一ID。

要保存有关活动状态的其他数据,您必须重写onSaveInstanceState()callback方法。 系统在用户离开你的活动时调用这个方法,并把你的活动被意外销毁时保存的Bundle对象传递给它。 如果系统稍后必须重新创build活动实例,则它将相同的Bundle对象传递给onRestoreInstanceState()和onCreate()方法。

正确的解决scheme是根据需要重写和实现Activity / Fragment的生命周期方法。

Google提供了两个例子:

  static final String STATE_SCORE = "playerScore"; static final String STATE_LEVEL = "playerLevel"; ... @Override public void onSaveInstanceState(Bundle savedInstanceState) { // Save the user's current game state savedInstanceState.putInt(STATE_SCORE, mCurrentScore); savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState); } Caution: Always call the superclass implementation of onSaveInstanceState() so the default implementation can save the state of the view hierarchy. 

而反向恢复操作:

 public void onRestoreInstanceState(Bundle savedInstanceState) { // Always call the superclass so it can restore the view hierarchy super.onRestoreInstanceState(savedInstanceState); // Restore state members from saved instance mCurrentScore = savedInstanceState.getInt(STATE_SCORE); mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); } 

一行 :看起来你的一些活动variables从内存中释放出来,因为Android操作系统需要Facebook应用程序的内存。

说明 :当前台应用程序需要更多的内存时,Android会从后台运行的应用程序中释放一些内存。 前台任务总是比后台应用程序具有更高的优先级。

所以,当你的应用程序处于后台时,可能会发生什么,它的一些variables已经失去了你在onResume()中使用的值。 正因为如此,当您再次将您的应用程序置于前台并由于某些代码不能正常工作时,它们将保留错误的值或默认值(您可以使用Sysout进行检查),因为它们是重新创build的。

希望你已经解决了这个问题,但是在你的代码中有一个错误,或许是相关的:

 if (backgroundThreadRunning = true) { backgroundThreadRunning = false; } 

您正在分配而不是在“if”语句中进行比较。 应该:

 if (backgroundThreadRunning == true) { backgroundThreadRunning = false; } 

要么

 if (backgroundThreadRunning) { backgroundThreadRunning = false; }