android - How to pass data between activities and perform arithmetic operation on that data -


i new android
have learned how pass data between activites putextra,getextra
in app,when pass data activity1 , activity2 main_activity , add both data , pass third activity (output)
instead of addition ,only value of either activity1 or activity2 passing output (value being passed 1 entered last during running app)
here code of app
mainactivity

    @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          intent j = getintent();         a= j.getdoubleextra("double1",0.0);          intent k = getintent();        b = k.getdoubleextra("double2",0.0);          result = a+b;     }       @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     }      public void on_click_activity1button(view view) {         intent intent1 = new intent(this,activity1.class);                 startactivity(intent1);      }      public void on_click_activity2button(view view) {         intent intent2 = new intent(this,activity2.class);         startactivity(intent2);      }      public void on_click_outputbutton(view view) {         intent intent3 = new intent(this,output.class);         intent3.putextra("result", result);          startactivity(intent3);      } }   

activity1

 @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity1);     }   public void on_click_activity1done(view view) {          edittext text1 = (edittext)  findviewbyid(r.id.activity1edittext);         a=double.parsedouble(text1.gettext().tostring());          intent fromactivity1 = new intent(this,mainactivity.class);         fromactivity1.putextra("double1", a);         startactivity(fromactivity1);     } } 

activity2.java

  @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity2);     }      public void on_click_activity2done(view view) {          edittext text2 = (edittext) findviewbyid(r.id.activity2edittext);         b=double.parsedouble(text2.gettext().tostring());          intent fromactivity2 = new intent(this,mainactivity.class);         fromactivity2.putextra("double2", b);         startactivity(fromactivity2);     } } 

output.java

   @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.output);         intent frommaindone = getintent();         double total = frommaindone.getdoubleextra("result",0.0);         textview show = (textview) findviewbyid(r.id.resulttextview);        show.settext("addition is" + total);     } }   

please me, , tell me wrong doing

you have use startactivityforresult method values 2 input activities , pass result output activity.change code below , try.

activity1

public class activity1 extends actionbaractivity {

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity1); }  public void on_click_activity1done(view view) {      edittext text1 = (edittext) findviewbyid(r.id.activity1edittext);     double b = double.parsedouble(text1.gettext().tostring());      intent result = new intent();     result.putextra("double1", b);     setresult(result_ok, result);     finish();  } 

}

activity2.java

public class activity2 extends actionbaractivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity2);     }       public void on_click_activity2done(view view) {          edittext text2 = (edittext) findviewbyid(r.id.activity2edittext);         double b = double.parsedouble(text2.gettext().tostring());          intent result = new intent();         result.putextra("double2", b);         setresult(result_ok, result);         finish();      } } 

mainactivity.java

public class mainactivity extends actionbaractivity {      private final int  activity1 = 1;     private final int  activity2 = 2;     private double result, double1,double2;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);      }      public void onactivityoneclick(view view) {         intent intent = new intent(this, activity1.class);         startactivityforresult(intent, activity1);     }      public void onactivitytwoclick(view view) {         intent intent = new intent(this, activity2.class);         startactivityforresult(intent, activity2);       }      public void on_click_outputbutton(view view) {         result = double1 + double2;         intent intent = new intent(this, output.class);         intent.putextra("result", result);         startactivity(intent);       }      @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         super.onactivityresult(requestcode, resultcode, data);         if(resultcode==result_ok){             switch(requestcode){             case activity1:                      double1 = data.getdoubleextra("double1", 0);                 break;             case activity2:                 double2 = data.getdoubleextra("double2", 0);                 break;             }                            }     } } 

output.java

public class output extends actionbaractivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.output);          double total = getintent().getdoubleextra("result", 0);              textview show = (textview) findviewbyid(r.id.resulttextview);         show.settext("addition is" + total);     }  } 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -