Skip going back to direct parent activity when pressed back

前端 未结 8 2019
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 22:37

I have got a small problem in an Android app I am working on :

There are 3 activities namely A , B , C and the invocation is in the following order : A -> B -> C.

8条回答
  •  感情败类
    2020-12-09 23:07

    You can start Activity C with startActivityForResult() and inside onActivityResult() finish Activity B.

    To start Activity C,

    Intent intent = new Intent(ActivityB.this, ActivityC.class);
    startActivityForResult(intent, 123);
    

    And override in Activity B

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
            if(requestCode == 123){
                if(resultCode == Activity.RESULT_OK){
                    finish();
                }
            }
        }
    

    And inside Activity C use setResult(Activity.RESULT_OK) before finish();

    UPDATE:

    Another way is to use FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP to start Activity A from Activity C.

    Intent intent = new Intent(ActivitC.this, ActivityA.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    

    One more way can be just finish() Activity B when you are starting Activity C. So, when you press back on Activity C it will directly move to Activity A as Activity B has already finished.

提交回复
热议问题