apex
apex

Reputation: 849

Android: skip activities while going between activities

I have 4 activities A, B , C and D.

The transition is A -> B -> C-> D. Now In activity D, I have opened a dialog fragment with an "Ok" button. When I click the "Ok" button I want to go back to actvity B. Till here I do not have any problem. But In activity B, when I click back button, I want to go back to activity A but instead it is redirecting to activity D. How can I achieve this?

P.S. I am currently working on Xamarin Android but I am sure the solution will work for both.

Upvotes: 1

Views: 469

Answers (6)

Saurabh Vadhva
Saurabh Vadhva

Reputation: 511

You can just finish previous activity using finish() method and onBackPressed() just redirecting the previous activity which you want. For examples: A-->B-->C-->D

onbackPressed of D class call C class using Intent.Like same thing in all classes.

Upvotes: 0

vishal patel
vishal patel

Reputation: 294

Set activity B launch mode "singleTask" in manifest file like

android:launchMode="singleTask"

After calling D to B activity your C and D activity will destroy. Your new intent data route through from this method

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

}

For more info read this link info

Upvotes: 0

Imtiaz Dipto
Imtiaz Dipto

Reputation: 352

in android :

@Override
public void onBackPressed() {
   Intent intent = new Intent(this, YourActivity.class);
   startActivity(intent);
}

in xamarin :

public override void OnBackPressed()
{
    var intent = new Intent(this, typeof(ActivityYouWhantToGetBackTo));
    intent.SetFlags(ActivityFlags.ClearTop);
    StartActivity(intent);
}

Upvotes: 0

Darpan
Darpan

Reputation: 5795

  • Use StartActivityForResult to start the activity D, and on press of OK button, user setResult to set some indicator that you want to go back to activity B directly.
  • In activity C's onActivityResult(), finish the activity C if flag that you set in activity D says it wants to go to Activity B directly.

It will finish activity C and you will end up on activity B directly.

Here is a very good explaination of how it works https://developer.android.com/training/basics/intents/result.html

Upvotes: 1

Mathias Kirkegaard
Mathias Kirkegaard

Reputation: 481

It seems like you need to navigate back to activity B with the ClearTop activity flag.

More info about:

https://developer.android.com/reference/android/content/Intent.html

and to use it:

yourIntet.AddFlags(ActivityFlags.ClearTop);

Upvotes: 2

Serhat Türkman
Serhat Türkman

Reputation: 403

in Activity B:

@Override
public void onBackPressed() {
   Intent intent = new Intent(this, AActivity.class);
   startActivity(intent);
}

You can find Xamarin format here

Upvotes: 0

Related Questions