Killing one activity from another

前端 未结 4 1576
后悔当初
后悔当初 2021-01-02 05:33

I have two activities A and B. B is a transparent pass through activity, and A is seen. I want to kill B by pressing a button A.

Here\'s what I\'ve tried so far:

相关标签:
4条回答
  • 2021-01-02 06:03

    Create static Class variable to save the instance: static SampleActivity sampleActivity;

    On Create of first Activity save the Instance, like this: incidenteActivity = this;

    Create a static method to get the instance:

    public static SampleActivity getInstance(){
        return   sampleActivity;
    }
    

    wherever you want call:

    SampleActivity.getInstance().finish();
    

    it really works, regards,

    0 讨论(0)
  • 2021-01-02 06:04

    I found a nice way to finish one activity from another, it is similar to what Lumis did. So if you want to close ActivityA from ActivityB you can do this:

    In ActivityA do:

    className = this.getClass().getName();
    

    and pass it on to AvtivityB. Then in ActivityB do:

    ((Activity) Class.forName(className).newInstance()).finish();
    

    You can put a string with the name of your class into className yourself, but it needs to be a full name with package too.

    0 讨论(0)
  • 2021-01-02 06:25

    You can stop the other activity by calling Activity.finish(). Alternatively, you can use Activity.finishActivity() to finish an activity started by startActivityForResult().

    0 讨论(0)
  • 2021-01-02 06:28

    You could try to directly kill an activity by calling a static method in that activity:

    Activity A should have a variable

     static ActivityA activityA;
    

    In onCreate state:

     activityA = this;
    

    and add this method:

    public static ActivityA getInstance(){
       return   activityA;
     }
    

    In activity B, call the function getInstance()

    ActivityA.getInstance().finish();     
    
    0 讨论(0)
提交回复
热议问题