我有一个在单独进程中运行的服务.我发现在主进程UI线程退出onDestroy()之后我的服务正在被销毁,即使我已经提供了带有绑定的应用程序上下文并指定了BIND_AUTO_CREATE.
在我的主进程'UI thread onCreate()中,我有这个绑定代码:
Intent intent = new Intent(mAppContext, MyService.class);
mAppContext.bindService(intent, mMyServiceConnection, Context.BIND_AUTO_CREATE);
Run Code Online (Sandbox Code Playgroud)
在我的主进程'UI thread onDestroy()中,我有这个无法解释的代码:
mAppContext.unbindService(mMyServiceConnection);
Run Code Online (Sandbox Code Playgroud)
请注意,我从不调用stopService().
Android的bindService()文档说:
只要存在调用上下文,系统就会认为该服务是必需的.
如果我正确地阅读,因为我提供了应用程序的上下文,系统认为该服务在应用程序的生命周期内是必需的.
我原以为应用程序的上下文可能会与onDestroy()一起消失.这就是Android的文档对getApplicationContext()的说法:
返回当前进程的单个全局Application对象的上下文.
如果应用程序的上下文与onDestroy()一起消失,那么我认为Android有一个大问题.问题是当旋转显示时,调用onDestroy()(紧接着是onCreate()).因此效果是当显示器旋转时 - 在我的情况下它经常发生! - 我的服务总是退出.
请注意,我的应用程序进程的pid永远不会更改,即它是相同的进程.鉴于getApplicationContext()的文档声明"当前进程",这一点很重要.
以下是我的调试日志显示的内容:
04-03 05:15:12.874:DEBUG/MyApp(841):main onDestroy
04-03 05:15:12.895:DEBUG/MyApp(847):service onUnbind
04-03 05:15:12.895:DEBUG/MyApp(847) ):service onDestroy
04-03 05:15:12.934:DEBUG/MyApp(841):main onCreate
04-03 05:15:12.966:DEBUG/MyApp(847):service onCreate
04-03 05:15:12.975:DEBUG/MyApp(847):service onBind
所以我的问题是:
1)我对绑定/解除绑定的理解是否正确?
2)当调用UI线程的onDestroy()时,有没有办法让我的服务不被破坏?
问题#2的黑客攻击永远不会解开.但是我不喜欢它,因为每次调用onDestroy()时我都会泄漏一个绑定.我可以"记住"我有一个泄漏的绑定,只泄漏那个,但后来我有级联黑客,它真的很难看.
一个问题,也许有点天真...
如果我在Activity onCreate()中启动2分钟的CountDownTimer并在用户单击按钮时使用finish()将其关闭,Android是否会自动取消它?
还是我需要在onDestroy()中显式调用cancel()?
我正在使用Robotium对Android应用进行功能测试.我想在调用onDestroy之后测试onResume行为. 这篇文章提示使用Instrumentation,但我无法让它工作.我已经包含以下内容,但是这会因IllegalStateException而失败.是否有可能销毁应用程序并重新启动它?
public class MainActivityFunctionalTest extends ActivityInstrumentationTestCase2<MainActivity> {
private Solo solo;
public MainActivityFunctionalTest() {
super(MainActivity.class);
}
public void testActionList() {
getInstrumentation().callActivityOnDestroy(solo.getCurrentActivity());
...
}
Run Code Online (Sandbox Code Playgroud)
结果如下:
java.lang.IllegalStateException: Must be called from main thread of process at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1373) at android.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1825) at android.app.Activity.performDestroy(Activity.java:5171) at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1109) at nl.handypages.trviewer.test.MainActivityFunctionalTest.testActionList(MainActivityFunctionalTest.java:81) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214) at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199) at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
Run Code Online (Sandbox Code Playgroud) 我有一个与BIND_AUTO_CREATE绑定到我的Activity的服务,并以START_STICKY开头.我没有显式调用startService().
由于当我的Activity被杀死并且onStop()方法不可行时,并不总是调用onDestroy()方法,因为当用户只需按下Home按钮时我不希望我的服务停止,我不知道何时取消绑定我的服务.
这是我的问题:
如果我希望我的服务在我的Activity活着时运行,并在我的Activity被杀死时停止,我应该在哪里取消绑定我的服务?
当前调用onDestroy()方法并调用unbindService()时,不会触发Service的onUnbind()方法.为什么?
根据Android片段生命周期,我希望在onDestroy
重新创建片段之后,或者至少onCreateView
再次调用片段.
我有一个活动A开始另一个活动B的结果和活动B创建一个片段F.
public class A extends FragmentActivity {
...
public void onButonClick() {
Intent intent = new Intent(this, B.class);
startActivityForResult(intent, REQUEST_B);
}
}
public class B extends FragmentActivity {
...
public void onCreate(Bundle savedInstanceState) {
...
this.currentFragment = Fragment.instantiate(this, name);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(this.view.getFragmentContainerId(), this.currentFragment, taskName);
transaction.commit();
}
}
public class F extends Fragment {
@override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.view = new MyView();
}
@override
public void onResume() { …
Run Code Online (Sandbox Code Playgroud) Angular文档说
ngOnDestroy():在Angular破坏指令/组件之前进行清理.取消订阅Observable并分离事件处理程序以避免内存泄漏.在Angular破坏指令/组件之前调用.
一些开发人员说组件属性(类变量)也应该设置为null以避免内存泄漏.这是真的?
export class TestComponent implements OnInit, OnDestroy {
public name: string;
constructor() { }
ngOnInit() {
this.name = 'John';
}
ngOnDestroy() {
// is this code really necessary.
this.name = null;
}
}
Run Code Online (Sandbox Code Playgroud) 有什么方法可以区分是否onDestroy()
会被称之后onPause()
?在5月的活动,我需要做不同的动作,当活动失去焦点而当活动正在下降,但即使活动正在下降onPause()
调用之前onDestroy()
我想要做不同的动作onPause()
,当活动失去焦点,并正在下降,当onDestroy()
是去是调用.
我的应用程序有 3 个类。第一类是启动画面,第二类包含播放列表列表,第三类包含播放列表的内容。选择播放列表时,第三个类开始才能显示播放列表内容。在二等舱我有:
@Override
protected void onStop() {
super.onStop();
System.out.println("onStop Playlist!!!!");
}
protected void onDestroy() {
super.onDestroy();
System.out.println("onDestroy Playlist");
}
Run Code Online (Sandbox Code Playgroud)
当第三堂课准备好开始时,我收到了 DDMS 消息:"onStop Playlist!!!!"
和"onDestroy Playlist"
。为什么要调用这个方法?不应该被称为唯一的onPause
方法吗?问题是我想在应用程序完成时停止一些计时器,但我不知道在这种情况下我可以在哪里停止计时器。任何的想法?
我这样称呼第三个班级:
Intent i = new Intent(getBaseContext(), ViewPlaylist.class);
i.putExtra("id", idPlaylist[position]);
i.putExtra("timer", timerPlaylist[position]);
startActivity(i);
finish();
Run Code Online (Sandbox Code Playgroud)
问题是我打电话finish()
?
我对super()
覆盖函数中的函数调用感到困惑.
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
Run Code Online (Sandbox Code Playgroud)
是的代码的影响,之前或之后写什么super.onDestroy()
或
super.onPause()
或其他超级功能在所有类型的android系统中被覆盖的方法呢?
android activity-lifecycle ondestroy onpause android-activity
我想要做的是知道用户何时从Android Launcher的菜单关闭应用程序,该菜单在主页按钮长按时打开:
我想在用户关闭应用程序时执行一些操作.
实际上,我希望用户每次离开应用程序时都会注销,但只要他能在此关闭应用程序,我就必须处理此事件然后进行操作.
我试图谷歌这个,但我找不到任何考虑这一点.
我不想覆盖onStop()或onDestroy(),只要用户可以回到应用程序然后,在这种情况下我不必进行更改.不建议这个.当用户完全关闭应用程序时,我只想进行更改.
所以我的问题是:
有没有什么onApplicationDestroy()
?
当用户以这种方式关闭应用程序时,是否可以与服务器建立连接并传递一些值?
android android-lifecycle ondestroy android-activity android-recents