Remove activities manually from Android app stack

2019-04-08 14:27发布

问题:

I been working on Android Native App , What i was trying to do is :

Activities - A -> B -> C  Then  A-> B -> C -> C  . 

From C Activity if it again point to C then i want to remove C , B from stack manually . On my back it should move only to A .

I tried finish() but problem is :

Activities - A -> B ->  C  Then  A-> B -> C -> C  on finish A -> B -> C  required state A-> C .

Is anyone know how to catch all activities in stack and remove specific activities from stack ??

回答1:

In Activity C, override onBackPressed and add in something like:

@Override
public void onBackPressed() {
    if (shouldGoBackToA) {  // There are various ways this could be set
        Intent intent = new Intent(this, AActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    } else {
        finish();
    }
}

FLAG_ACTIVITY_CLEAR_TOP will cause it to go down the stack to the existing copy of A Activity instead of starting a new one. From the docs:

public static final int FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.



回答2:

While calling intent pass a flag called actvity clear top like this:

Intent newIntent=new Intent(this,MainActivity.class);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(newIntent);


回答3:

You can use this : In A activity while passing to B activity, the intent should be added with a flag FLAG_ACTIVITY_NO_HISTORY like this,

Button b=(Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent newIntent=new Intent(AActivity.this,Bactivty.class);
                newIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(newIntent);
            }
        });

While moving to CActivity:

Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent newIntent = new Intent(Bactivty.this, CActivity.class);
                startActivity(newIntent);
            }
        });

On backpress will take you to AActivity now.



回答4:

Step 1: Start activty for result A -> B -> C1 -> C2.. Call your Activity with startActivityForResult

Intent intent = new Intent(yourActivity.this, nextActivity.class);
startActivityForResult(intent, 1);

Step 2: In C2 specify that you want to go back to A.. Whenever you are done with your activity write the below code

Intent i = getIntent();
i.putString("Result","GottoA");
setResult(Activity.RESULT_OK, i);
finish();

Step 3: Whenever C2 finishes , previsus stack activit's onActivityResult is called.. so u can check in C1 and B onActivityResult whether you have set any result bck.. and finish accordingly and impliment the following code in Activity B and c

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     Intent i = getIntent();    
     if (resultCode == RESULT_OK && i.getString("Result","null").equals"GottoA") { 
        i.putString("Result","GottoA");
        setResult(RESULT_OK, i);
        finish();
       }
    }


回答5:

this complete example may help you...

public class ActivityA extends Activity {

    public static final int ID_TEXTVIEW = 0xDEAF1;
    public static final int ID_BUTTON = 0xDEAF2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View contentView = getContentView(this);
        TextView textView = (TextView) contentView.findViewById(ID_TEXTVIEW);
        textView.setText("ActivityA");
        setContentView(contentView);

        final Button button = (Button) contentView.findViewById(ID_BUTTON);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityA.this, ActivityB.class);
                startActivity(intent);
            }
        });
    }

    public static View getContentView(Context context) {
        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;

        TextView textView = new TextView(context);
        textView.setLayoutParams(layoutParams);
        textView.setId(ID_TEXTVIEW);
        layout.addView(textView);

        Button button = new Button(context);
        button.setText("Next");
        button.setLayoutParams(layoutParams);
        button.setId(ID_BUTTON);
        layout.addView(button);
        return layout;
    }
}



public class ActivityB extends Activity {

    public static final String ACTION_FINISH = "com.myapp.test2.ACTION_FINISH";

    public ActivityB() {
    }

    private FinishReceiver finishReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View contentView = ActivityA.getContentView(this);
        final TextView textView = (TextView) contentView
                .findViewById(ActivityA.ID_TEXTVIEW);
        textView.setText("ActivityB");
        setContentView(contentView);

        final Button button = (Button) contentView
                .findViewById(ActivityA.ID_BUTTON);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityB.this, ActivityC.class);
                startActivity(intent);
            }
        });

        finishReceiver = new FinishReceiver();
        IntentFilter filter = new IntentFilter(ACTION_FINISH);
        registerReceiver(finishReceiver, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(finishReceiver);
    }

    private class FinishReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ACTION_FINISH)) {
                finish();
            }
        }
    }
}


public class ActivityC extends Activity {

    public ActivityC() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View contentView = ActivityA.getContentView(this);
        final TextView textView = (TextView) contentView
                .findViewById(ActivityA.ID_TEXTVIEW);
        textView.setText("ActivityC");
        setContentView(contentView);

        final Button button = (Button) contentView.findViewById(ActivityA.ID_BUTTON);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityB.ACTION_FINISH);
                sendBroadcast(intent);
                intent = new Intent(ActivityC.this, ActivityC.class);
                startActivity(intent);
                finish();
            }
        });
    }
}


回答6:

In Activity C, when back button is pushed start activity A like this:

@Override
public void onBackPressed() {
  Intent intent = new Intent(getApplicationContext(), A.class);
  intent.putExtra("EXIT", true);
  startActivity(intent);
}

Then in Activity A's onCreate() do this

if (getIntent().getBooleanExtra("EXIT", false)) {
    finish();  
}