Reputation: 65
Can anyone please tell me what should I do in the following scenario. I have four activities Ac1, Ac2, Ac3 and Ac4, each activity has a Cancel button, which when pressed should take to the Ac1. Also back button should traverse back to previous activity. Ac1 is the start and Ac4 is the end in the flow.
I had used FLAG_ACTIVITY_NEW_TASK in the Cancel button and in the last Ac4, to go back to Ac1. But this was not working as expected. So to do this I have a broadcast register to stop the activities when Cancel button is pressed in any of the activities. So on recieving the broadcast reciever I would call finish() to close the stacked activities. Everything works fine when the developer options is not set(Dont keep activties). When this option is set, 1. Ac1 is moved to Ac2. 2. Ac2 is started, but Ac1 is cleared. 3. Ac2 is moved to Ac3 4. Ac3 is started and Ac2 is cleared. 5. When Cancel is called, broadcast reciever is sent to close activity. 6. Surprisingly Im seeing Ac2 is called, instead of Ac1. I could see onCreate of Ac2 is being called.
I dont know how to get rid of this situtation. Can anyone help me out of this situtation.
Thanks
Upvotes: 0
Views: 934
Reputation: 11522
If I understand right, you want to support a stack of activities that may grow from A1 up to A4. The user may use the back button to transition back from A(N+1) to A(N), and may hit a Cancel button to clear the entire stack from A(N) down to A2, leaving only A1.
(Sorry if that's no clearer than your original description :-)
My first approach would be to use Activity.startActivityForResult
to start the child activities. When a child activity exits -- whether by the back button, the Cancel button, or a normal exit -- that child's immediate parent can look at the result code that is passed back to onActivityResult
, and terminate itself if the result code indicates that the Cancel button was selected.
Have you tried anything like this?
Upvotes: 1
Reputation: 170
I would do it like this:
class Activity2{
int KILL_ACTIVITY=555;
startActivityForResult(new Intent(Activity2.this,Activity3.class), 0);
@Override onActivityResult(... , int result){
if(result==KILL_ACTIVITY)
finish();
}
class Activity3{
int KILL_ACTIVITY=555;
@Override onClick(View v){
if(v.getId()==R.id.button){
setResult(KILL_ACTIVITY);
finish();
}
}
startActivityForResult(new Intent(Activity3.this,Activity4.class), 0);
@Override onActivityResult(... , int result){
if(result==KILL_ACTIVITY)
finish();
}
}
Upvotes: 0
Reputation: 4383
try to link cancel button to ac1 try to clear old activity stack and jump to ac1
Intent intent = new Intent(this, ac1.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Upvotes: 0
Reputation: 715
Broadcast is a bit overkill and FLAG_ACTIVITY_NEW_TASK is not what you are looking for.
Try to call Ac1 in cancel button with intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
Upvotes: 1