0

How to pass value from 1 class method to another class activity

strong text

   public class A extends Activity{
    int value;

    protected void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
       .
       .
       . 
     methodA();
    } 

    public void methodA(){

       if (condition){
            value =1
       }
       else if (condition){
            value =2
           }
       else{
            value =3
           }
        }

        }
        }

class B

Public class B extends Activity{

  protected void onCreate(Bundle savedInstanceState){
       int val;
       super.onCreate(savedInstanceState);
       .
       . 
     //how do we can get val = value
    } 

}

Question is how can val from class B get the value from class A, will intent work for these condition?

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
JohnaHalim
  • 200
  • 2
  • 12
  • Possibly you are looking for http://stackoverflow.com/questions/3510649/how-to-pass-a-value-from-one-activity-to-another-in-android – Gaurav Gupta Apr 15 '14 at 09:21
  • http://stackoverflow.com/questions/2906925/android-how-do-i-pass-an-object-from-one-activity-to-another – maxime1992 Apr 15 '14 at 09:21

3 Answers3

0

Use intent for this purpose

public void methodA(){

   if (condition){
        value =1
   }
   else if (condition){
        value =2
       }
   else{
        value =3
       }
      Intent intent = new Intent(this,B.class);
      intent.putExtra("key",value);
      startActivity(intent); 
    }

Then

Public class B extends Activity{
protected void onCreate(Bundle savedInstanceState){
   int val;
   super.onCreate(savedInstanceState);
   int value = getIntent().getIntExtra("key",0);  
   .
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • i have already try this and it not working, it ill give out default value that is = 0 it not going to take out the value values to class B. – JohnaHalim Apr 17 '14 at 02:42
  • @user2930725 then the value is not sent properly coz `getIntExtra("key",0);` the default is 0 – Raghunandan Apr 17 '14 at 03:36
0

When starting your activity B you can send the value with the Intent.

In A (when starting B):

Intent intent = new Intent(A.this, B.class);
intent.putExtra("MYVALUE", val);
startActivity(intent);

In B:

val = getIntent().getIntExtra("MYVALUE");
Pphoenix
  • 1,423
  • 1
  • 15
  • 37
0

There are several options to pass values. However passing integer is not best pattern in my private opinion. I used to create class ex. call it ContainerWithData that implements Serializable (may be Parcalable). I would prefer thge first one as it can be used later on by other libraries like Gson for api sending or mappers to convert.

Class Aa extends Activity {

   public initAndRunBeAcitvity(int parameterInt){
      ContainerWithData data = new ContainerWithData();
      data.setIntegerValue(parameterInt);
      runBe(data);
   }

   public void runBe(ContainerWithData data){
      Intent intent = new Intent(Aa.this,Be.class);
      intent.putExtra("MyBeData",data);
      startActivity(intent); 
   }

}

Class Be extends Activity {

   protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      prepareInitParameters();
   }

   private void prepareInitParameters(){
     getInitParameters();
     checkInitParameters();
   }

   private void getInitParameters(){
     ContainerWithData value = getIntent().getSerializableExtra("MyBeData");
     this.value = value;
   }


   private void checkInitParameters(){
      if (value==null){
               //Perform any initialization data, 
               //message or postDelayed closing of activity
       } 
   }


}

Class ContainerWithData implements Serializable {

   @Getter
   @Setter
   private Integer integerValue; 
   //can be null if not setted, 
   //so should be checked by if conditions later on controller of view logic

}

So proposition is to use

getIntent().getSerializableExtra("MyBData");

and

getIntent().getSerializableExtra("MyBData"); together with using model to pass data.

With such pattern many times I had avoid spending more time to add new values as application grows.

  • I think this is what im looking for let me have a try first :) – JohnaHalim Apr 17 '14 at 02:51
  • @user2930725 parcelable is better than serializing. if you are passing primitive data types there is no need for serializing – Raghunandan Apr 17 '14 at 03:37
  • You should always look to put serializable and not promitive data, remeber that it is simplier to extend later by addind some extra values inside. – elektronika Aug 26 '16 at 13:48