Sunday, 15 January 2012

java - Trying to exit in android. Stack is not correctly cleared -



java - Trying to exit in android. Stack is not correctly cleared -

i have been spending lots of hours figuring reason why top of stack not cleared yet.

well tried following:

intent intent = new intent(actionbaractivity.this, mainactivity.class); intent.addcategory(intent.category_home); intent.setflags(intent.flag_activity_new_task); intent.addflags(intent.flag_activity_clear_top); startactivity(intent);

and turned me mainactivity. when seek press button, programme not exit, instead turns me first page of actionbaractivity.

let me more specific:

in mainactivity phone call actionbaractivity. in actionbaractivity have search bar , come in query there , print value of query.

if think how work below:

mainactivity -> actionbaractivity1 -> actionbaractivity2 -> actionbaractivity3 -> ..

in actionbaractivity have alternative brings me mainactivity.

so said when run application way above, bring me mainactivity.

nice far, when press button expect exit, instead goes actionbaractivity1. uncertainty stack not erased.

what should in case. thanks

i'm assuming you've seen post: android: clear stack , not want

the flag_activity_new_task creating new task stack. when nail "back" activity in stack, android goes previous task - has calling activity @ top.

if remove flag, mainactivity in task stack , brought top. , "back" removes , brings calling activity. far, expected android behavior.

edit: since commented have 2 activities, 1 alternative "finish()" actionbaractivity after starts mainactivity this:

startactivity(intentmainactivity); finish();

to behavior want many activities or in situations, need utilize set_result , finish activities in stack when nail button go mainactivity. in other words, android assumes want maintain activities when new ones start. in case don't, have tell activity destroy itself.

i.e. phone call setresult(done) , utilize startactivityforresult , check "done" flag in onactivityresult method.

then this:

mainactivity -> actionbaractivity1 -> actionbaractivity2 -> actionbaractivity3 -> mainactivity

becomes:

mainactivity -> actionbaractivity1 -> actionbaractivity2 -> actionbaractivity3 -> phone call "done" destroy actionbaractivity3 destroy actionbaractivity2 destroy actionbaractivity1 mainactivity remains

for example, in actionbaractivity2:

startactivityforresult(intentactionbaractivity3, actionbaractivity2_id);

then in actionbaractivity3:

public void exitbutton(view view) { setresult(my_done_flag); finish(); }

and in actionbaractivity2:

protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == actionbaractivity2_id && resultcode == my_done_flag) { setresult(my_done_flag, data); finish(); } }

and on activities want "close" when button pressed.

java android

No comments:

Post a Comment