Sending data back to the Main Activity in Android

后端 未结 12 1942
后悔当初
后悔当初 2020-11-22 01:36

I have two activities: main activity and child activity.
When I press a button in the main activity, the child activity is launched.

Now I want to send some dat

12条回答
  •  花落未央
    2020-11-22 02:11

    There are some ways of doing this. 1. by using the startActivityForResult() which is very well explained in the above answers.

    1. by creating the static variables in your "Utils" class or any other class of your own. For example i want to pass studentId from ActivityB to ActivityA.First my ActivityA is calling the ActivityB. Then inside ActivityB set the studentId (which is a static field in Utils.class). Like this Utils.STUDENT_ID="1234"; then while comming back to the ActivityA use the studentId which is stored in Utils.STUDENT_ID.

    2. by creating a getter and setter method in your Application Class.

    like this:

    public class MyApplication extends Application {
    
        private static MyApplication instance = null;
        private String studentId="";
    
        public static MyApplication getInstance() {
            return instance;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            instance = this;
        }
    
        public void setStudentId(String studentID){
            this.studentId=studentID;
        }
    
        public String getStudentId(){
            return this.studentId;
        }
    }
    

    so you are done . just set the data inside when u are in ActivityB and after comming back to ActivityA , get the data.

提交回复
热议问题