7

can anyone help what is the problem in my code

Activiy 1:

int view=1;
TabFunctionality.setFirstTabFunctionality(TabFunctionality.FIRST_TAB, 1);
Intent intent = new Intent(AdultTeeth.this, MainScreen.class);
Bundle b = new Bundle();
b.putInt("TEXT", view);                 
intent.putExtras(b);    
startActivityForResult(intent, TEETH_VIEW);
finish();

Activity 2:

Bundle b = this.getIntent().getExtras(); int view=b.getInt("TEXT");
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
user828948
  • 151
  • 1
  • 3
  • 8

7 Answers7

20

You can directly use putExtra also.

Activity 1

Intent intent = new Intent(AdultTeeth.this, MainScreen.class);
intent.putExtra("int_value", int_variable);
startActivity(intent);

Activity 2

Intent intent = getIntent();
int temp = intent.getIntExtra("int_value", 0); // here 0 is the default value
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • 1
    Hi i should get the value of view ,but amm getting defaultvalue as value of view in second activity when i print the value of view – user828948 Oct 01 '11 at 09:46
  • In above u taken int_variable instead of that i took view as my integer value ;int view =1; – user828948 Oct 01 '11 at 09:50
  • when i try to print temp its giving 0 – user828948 Oct 01 '11 at 09:51
  • TabFunctionality.setFirstTabFunctionality(TabFunctionality.FIRST_TAB, 1); Intent intent = new Intent(AdultTeeth.this, MainScreen.class); intent.putExtra("TEXT", view); startActivity(intent); finish(); view=1 is global variable – user828948 Oct 01 '11 at 10:06
  • activity 2: Intent i1 = getIntent(); Show=i1.getIntExtra("TEXT",10); System.out.println("v-------------->"+Show); – user828948 Oct 01 '11 at 10:06
4

Passactivity:

 Intent i = new Intent(view.getContext(), Passactivity.class);        
 i.putExtra("font",selected_font);
 startActivity(i);

Receving activity

private int my_size;

 Intent i = getIntent();
     my_size = i.getIntExtra("size",20); // 20  for default value.
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
1

The answers given are here aren't wrong but they are incomplete in my opinion. The best way to do this with validations is to make sure that the extra is taken from the previous Activity as well as the savedInstanceState which is the Bundle data received while starting the Activity and can be passed back to onCreate if the activity needs to be recreated (e.g., orientation change) so that you don't lose this prior information. If no data was supplied, savedInstanceState is null.

Sending data -

Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("name", "Daenerys Targaryen");
intent.putExtra("number", "69");
startActivity(intent);

Receiving data -

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myactivity);

    int no;
    String na;

    if(savedInstanceState == null){
        Bundle extras = getIntent().getExtras();

        if(extras != null){
            no = Integer.parseInt(extras.getString("number"));
            na = extras.getString("name");
        }

    }else{
        no = (int) savedInstanceState.getSerializable("number");
        na = (String) savedInstanceState.getSerializable("name");
    }

    // Other code
}
Sanved
  • 921
  • 13
  • 19
0

in first activity

 Intent i = new Intent(name_class.this, name_class.class);
 i.putExtra("valu1",valu1);
 i.putExtra("value2", valu2);

second Activity ::

    Bundle bundle = this.getIntent().getExtras();
    String valu1 =bundle.getString("value1");
    String value2 = bundle.getString("value2");
Noaman Akram
  • 3,680
  • 4
  • 20
  • 37
Anil M H
  • 3,332
  • 5
  • 34
  • 51
0

just pass it like a string and typecast it

  • 1
    Welcome to SO! While this may answer the question it is of low quality. You should consider adding an example and/or link that would be helpful to people with a similar problem. – codeMagic Mar 07 '14 at 20:48
0

in first activity ::

intent1.putExtra("key",int_score);
startActivity(intent1);

second Activity ::

  Intent i1 = getIntent();  
  int temp = i1.getIntExtra("key",1);                                                                                                               int temp = i1.getIntExtra("tranningscore2", 1);
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
0

use this code may its work.

intent.putExtra("Text", view);

activity 2:

int i = getIntent().getIntExtra("Text", 0);

where 0 is the default value.

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
mathlearner
  • 7,509
  • 31
  • 126
  • 189
  • Hi i should get the value of view ,but amm getting defaultvalue as value of view in second activity when i print the value of view – user828948 Oct 01 '11 at 09:46
  • i dont know why its giving value of view 0 but i use this code many times it give the appropriate value which i put in activity 1.please make sure you dont put the value of view in Intent.putExtra("Text",view);check the value of view first in first activty – mathlearner Oct 01 '11 at 09:57
  • Intent i1 = getIntent(); Show=i1.getIntExtra("TEXT",10); System.out.println("v-------------->"+Show); – user828948 Oct 01 '11 at 10:07