dev_android
dev_android

Reputation: 8818

Android activity stack and back function problem

I have four activities, A, B, C and D. app starts with activity A, then it goes to B by using explicit intend, then C and then D in same way. From D, if I want to come back to directly B or A, how it can be done?

Upvotes: 1

Views: 490

Answers (2)

surya
surya

Reputation: 487

in Activity A

public void onCreate()
{ //when u want to start new activity
startActivity(intent); //starting activity to B
}

in Activity B

public void onCreate()
{
//when u want to start new activity
startActivityForResult(intent, 10//any code value); //starting activity to c
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode==25)
{
finish();
}
}

in Activity C

public void onCreate()
{
//when u want to go back to Actitvity A
setResult(25);
finish();
}

Explanation

1)in activity "A" ur starting one fresh activity to "B"
2)in "B" activity ,ur starting one activity to "C" ,using startActivityForResult method
3)in "C" activity ,when u finish,it obviously go to "B" activity,with result code which u set.and if it's match it will close "B" activity ,and go to "A" activity

4)This is one simple trick to skip one or more activity

Upvotes: 0

David Olsson
David Olsson

Reputation: 8225

For example, in C. If you call finish() after you send the intent to start Activity D when the user presses the back button in Activity D she/he will be sent to Activity A or B depending where you started activity C. Another way is to set flag to clear top like this:

Intent intent  = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        
startActivity(intent);

The easiest way is to call finish() depending on how you want the flow in your app to be.

Upvotes: 3

Related Questions