android - Why onDestroy inActivity A not being called after a call to finish() in ActivityB -
the next process simple understand , reproduce leads bug:
- activitya starts activityb in
oncreate()
method - activityb created , call
finish()
in onresume() method - activityb
ondestroy()
called - activitya
onresume()
called - and here in activitya, click menu button call
finish()
- or press key. - activitya removed
ondestroy()
not called , still living ( adb shell dumpsys 'mypackagename' indicates many living activities )
code
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="gleroy.com.algo"> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme"> <activity android:name=".activity.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="gleroy.com.algo.activity.fakea" android:label="@string/app_name"></activity> <activity android:name="gleroy.com.algo.activity.fakeb" android:label="@string/app_name"></activity> </application> </manifest>
activity :
public class fakea extends activity { private final static string tag = fakea.class.getcanonicalname(); @override protected void oncreate(bundle savedinstancestate) { log.d(tag, "oncreate, taskid :" + gettaskid()); super.oncreate(savedinstancestate); intent intent = new intent(fakea.this, fakeb.class); startactivity(intent); } @override protected void onresume() { log.d(tag, "onresume"); super.onresume(); } @override protected void ondestroy() { log.d(tag, "ondestroy"); super.ondestroy(); } @targetapi(build.version_codes.honeycomb) @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); super.oncreateoptionsmenu(menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.stop_session_menu_item: /* stop , quit */ finish(); return false; } return super.onoptionsitemselected(item); } }
activity b :
public class fakeb extends activity { private final static string tag = fakeb.class.getcanonicalname(); @override protected void oncreate(bundle savedinstancestate) { log.d(tag, "oncreate, taskid :"+gettaskid()); super.oncreate(savedinstancestate); } @override protected void onresume() { super.onresume(); log.d(tag, "onresume, isfinishing :" + isfinishing()); finish(); } @override protected void ondestroy() { super.ondestroy(); log.d(tag, "ondestroy"); } }
activity started mainactivity contains simple button :
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); findviewbyid(r.id.btn).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(mainactivity.this, fakea.class); startactivity(intent); } });
so know can't sure ondestroy()
gonna called here activitya leaking.
also observed if use timer
, timertask
delay startactivity
in activitya or finish()
in activityb don't have bug anymore.
here events :
- fakea oncreate, taskid :154
- fakea onresume
- fakea onpause, isfinishing : false
- fakeb oncreate, taskid :154
- fakeb onresume, isfinishing :false
- fakea onresume
- fakeb ondestroy
- call finish or press key : fakea onpause, isfinishing : true
in place of finish()
try finishaffinity()
. far know: finish()
destroys current live activity
while, finishaffinity()
destroys active activities
.
Comments
Post a Comment