java - Issues re-starting a finish()ed android activity -
i have 2 android activities 1 single button on both.
the activity detailsactivity
main
activity.
public class detailsactivity extends activity { @override public void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.details_activity_layout); } @override public void onresume() { super.onresume(); } public void gotosubdetails(view view) { intent intent = new intent(this, subdetailsactivity.class); startactivity(intent); finish(); } }
and subdetailsactivity
follows:
public class subdetailsactivity extends activity { @override public void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.sub_details_activity_layout); } @override public void onresume() { super.onresume(); } public void gotodetails(view view) { intent intent = new intent(this, detailsactivity.class); startactivity(intent); // restarting finish()ed activity here. finish(); } }
and how mentioned button in details_activity_layout.xml
detailsactivity.java
:
<button android:id="@+id/details_submit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/to_sub_details" android:onclick="gotosubdetails" />
and how have mentioned button in sub_details_activity_layout.xml
subdetailsactivity.java
:
<button android:id="@+id/sub_details_submit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/back_to_details" android:onclick="gotodetails" />
these both activities in androidmanifest.xml
:
<activity android:name="detailsactivity"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="subdetailsactivity"> </activity>
when click button
in detailsactivity
, activity finish()
es , subdetailsactivity
starts up.
but when click button
in subdetailsactivity
finish()
ed detailsactivity
, app straight away crashes on lg l90 phone.
where going wrong? help? please.
ps: cannot post log cat report did not test on mac directly on phone. don't have emulator. compiling code on terminal , transferring .apk
file on phone via bluetooth.
edit: ok guys. funny!
i removed
android:onclick="gotosubdetails"
and
android:onclick="gotodetails"
from details_activity_layout.xml
, sub_details_activity_layout.xml
respectively. , loaded android.widget.button
's in detailsactivity.java
, subdetailsactivity.java
, added android.view.view.onclicklistener
on both of them , overrode onclick(view)
method.
this did in detailsactivity.java
:
public class detailsactivity extends activity { @override public void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.details_activity_layout); button button = (button)findviewbyid(r.id.details_submit); button.setonclicklistener(new detailssubmitlistener()); } @override public void onresume() { super.onresume(); } public class detailssubmitlistener implements onclicklistener { @override public void onclick(view view) { intent intent = new intent(this, subdetailsactivity.class); startactivity(intent); finish(); } } }
and did in subdetailsactivity.java
class:
public class subdetailsactivity extends activity { @override public void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.details_activity_layout); button button = (button)findviewbyid(r.id.sub_details_submit); button.setonclicklistener(new subdetailssubmitlistener()); } @override public void onresume() { super.onresume(); } public class subdetailssubmitlistener implements onclicklistener { @override public void onclick(view view) { intent intent = new intent(this, detailsactivity.class); startactivity(intent); finish(); } } }
and miraculously worked fine.
so problem .xml
attributes in layout files? , how different hardcoded listener in .java
file? thing has confused me. finish()
ed activities can re-started. thats discovered change in program. please shed light of knowledge on this.
aditya,
if want traverse between 2 activities in case, should never finish details activity. instead without finishing details activity, start subdetails activity. if want come back, no coding required. if user presses button, subdetails activity finished , detailsactivity come foreground.
finishing 1 activity , starting again, unnecessary overhead. avoid it. finish activity if absolutely necessary.
also, there no way restart finished activity.
Comments
Post a Comment