Finish any previous activity in stack from current activity?

前端 未结 4 763
别那么骄傲
别那么骄傲 2020-12-29 12:27

How to finish any previous activity in application stack (at any level , I mean not immediate parent) , from current activity like on some particular event I want to invalid

4条回答
  •  礼貌的吻别
    2020-12-29 13:14

    I know this answer may be late, but I'm still going to post it in case someone is looking for something like this.

    What I did is I declared a static handler in in ACTIVITY_A

    public static Handler h;
    

    and in my onCreate() method for ACTIVITY_A, I have

    h = new Handler() {
    
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
    
                switch(msg.what) {
    
                case 0:
                    finish();
                    break;
    
                }
            }
    
        };
    

    Now, from any activity after this one, such asACTIVITY_B, or ACTIVITY_C I can call

    ACTIVITY_A.h.sendEmptyMessage(0);
    

    which then calls finish() in ACTIVITY_A and ta-da! ACTIVITY_A is finished from a different activity.

提交回复
热议问题