Reputation: 247
I have a few activities and can't figure out a way to make it work with the backstack.
Is there a way to do this:
MainActivity ->(intent) subActivity ->(intent)subsubActivity->(back press)subActivity->*(back press)MainActivity
*This is where I am having problems. Since I am coming from my subsubActivity, even thought I used android:noHistory="true" in the manifest, it doesn't go back to main activity.
Thanks for any help!
Upvotes: 1
Views: 1294
Reputation: 6834
This is a perfect candidate for using a combination of startActivityForResult
and overriding onActivityResult
in the calling Activity
.
Assuming you have Activity A
, which starts Activity B
(which we cannot move back to), which starts Activity C
:
Activity A
will call Activity B
the way you are now. Activity B
, however, will call startActivityForResult(Intent, int)
instead of just startActivity(Intent)
. This way, when we return from Activity C
, we can call finish()
on Activity B
and return to Activity A
, like so:
public class ActivityB extends Activity {
private static final int REQUEST_CODE_ACTIVITY_C = 1001;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_CODE_ACTIVITY_C) finish(); // If coming back from Activity C, finish()
}
private void openActivityC(){
Intent intent = new Intent(this, ActivityC.class);
startActivityForResult(intent, REQUEST_CODE_ACTIVITY_C);
}
}
Now when you call openActivityC()
, you're ensuring that onActivityResult()
will be called when returning from Activity C
, thus allowing you to end the Activity
and return back to Activity A
.
You can provide even more specific actions (such as setting/checking the result code (e.g. if it canceled (Activity.RESULT_CANCELED
) or successful (Activity.RESULT_OK
)) for statuses, to better determine what that calling Activity
should do.
Hopefully that helps explain it a bit.
Edit: Also an afterthought, if there's no chance you'll ever want to go back to Activity B, then @Kay 's solution of just calling finish() after firing the Intent for Activity C would be the simplest approach.
Upvotes: 1
Reputation: 2452
If i get what you want You can use startActivities with an array of intents which will be fired one after another activity1->activity2->activity3 -back-activity2 -back-acitivty1
Upvotes: 0