lancer025
lancer025

Reputation: 157

Removing previous activity from current activity in android

I have a stack of activities like A->B->C->D... Back function is enabled on all activities. Now on a specific action on activity D, I would like to move to activity B i.e. D&C gets finished now and activity B is resumed. How to achieve such transition ?

Upvotes: 1

Views: 993

Answers (4)

akhil Rao
akhil Rao

Reputation: 1169

Try adding this code inside some action of D Activity

 C close = new C();
 close.finish();
 D.this.finish();

And in activity tag of manifest file add this attribute

android:launchmode="singleTop" //Or singleTask

So that you can close both c and D activity and resume into B.

Upvotes: 0

Ahmad Aghazadeh
Ahmad Aghazadeh

Reputation: 17131

Just to clarify, use this API < 11:

    Intent intent= new Intent(this, Login2.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(Intent);

In API level 11 a new Intent Flag was added just for this: Intent.FLAG_ACTIVITY_CLEAR_TASK

    Intent intent= new Intent(this, Login2.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(Intent);

Upvotes: 0

Mrugesh
Mrugesh

Reputation: 4517

If you want to follow proper and ideal flow , go for "onActivityResult" method. Suppose for your example when B goes to C, at that time it will start activity C using startActivityForResult and when C goes to D then again C will use statActivityForResult to go do D.

For your case,

public class B extends Activity
{
   public static int RESULT_CODE=111;

   public void onCreate(Bundle SavedInstance)
   {
   ...
   ....
   Intent intent = new Intent(B.this,C.class);
   startActivityForResult(intent,REQUEST_CODE);
   }


   public void onActivityResult(int requestCode,int resultCode,Intent data)
   super.onActivityResult(requestCode, resultCode, data);
   if(requestcode == C.RESULT_OK)
       {

       //do you work when D is finished and comes back to B
        }

    }


public class C extends Activity
{
     public static int RESULT_CODE=222;

     public void onCreate(Bundle SavedInstance)
     {
     ...
     ....
     Intent intent = new Intent(C.this,D.class);
     startActivityForResult(intent,REQUEST_CODE);
     }


    public void onActivityResult(int requestCode,int resultCode,Intent data)
    super.onActivityResult(requestCode, resultCode, data);
    if(requestcode == D.RESULT_OK)
    {
    setResult(RESULT_OK);
    }

   } 


    public class D extends Activity
    {
      public static int RESULT_CODE=333;

      public void onCreate(Bundle SavedInstance)
      {
      ...
      ....

      }


      //your back click function

      public void onBackClick(View v)
      {
      setResult(RESULT_OK);
   }

Upvotes: 0

jaibatrik
jaibatrik

Reputation: 7523

From Activity D, call Activity B with proper flags in the Intent. Intent.FLAG_ACTIVITY_CLEAR_TOP should do it.

Example code:

Intent intent = new Intent(this, B.class)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();

Upvotes: 1

Related Questions