Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a String from one Activity to another Activity in Android

Tags:

android

This is my string:

private final String easyPuzzle ="630208010200050089109060030"+
                                 "008006050000187000060500900"+
                                 "09007010681002000502003097";

I want to show this string on the another activity at the 9*9 sudoku board.

like image 986
Chandan dhamat Avatar asked Jul 15 '11 13:07

Chandan dhamat


People also ask

How Pass String from activity to another activity in Android?

putExtra() method is used for send the data, data in key-value pair key is variable name and value can be Int, String, Float etc. getStringExtra() method is for getting the data(key) which is send by above method. according the data type of value there are other methods like getIntExtra(), getFloatExtra()

How do I send String from one activity to another in flutter?

In activity1 , in onCreate() , you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getString() on it : Bundle bundle = getIntent(). getExtras(); String message = bundle. getString("message");


4 Answers

You need to pass it as an extra:

String easyPuzzle  = "630208010200050089109060030"+
                     "008006050000187000060500900"+
                     "09007010681002000502003097";

Intent i = new Intent(this, ToClass.class);
i.putExtra("epuzzle", easyPuzzle);
startActivity(i); 

Then extract it from your new activity like this:

Intent intent = getIntent();
String easyPuzzle = intent.getExtras().getString("epuzzle");
like image 197
Kenny Avatar answered Oct 18 '22 05:10

Kenny


In activity1

    String easyPuzzle  = "630208010200050089109060030"+
                 "008006050000187000060500900"+
                 "09007010681002000502003097";

    Intent i = new Intent (this, activity2.class);

    i.putExtra("puzzle", easyPuzzle);
    startActivity(i);

In activity2

    Intent i = getIntent();
    String easyPuzzle = i.getStringExtra("puzzle");
like image 20
Saneth Chandrasekara Avatar answered Oct 18 '22 03:10

Saneth Chandrasekara


private final String easyPuzzle ="630208010200050089109060030"+
                             "008006050000187000060500900"+
                             "09007010681002000502003097";
Bundle ePzl= new Bundle();
ePzl.putString("key", easyPuzzle);

Intent i = new Intent(MainActivity.this,AnotherActivity.class);
i.putExtras(ePzl);
startActivity(i);

Now go to AnotherActivity.java

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

    Bundle p = getIntent().getExtras();
    String yourPreviousPzl =p.getString("key");

}

now "yourPreviousPzl" is your desired string.

like image 8
Atiar Talukdar Avatar answered Oct 18 '22 04:10

Atiar Talukdar


In ActivityOne,

Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
intent.putExtra("data", somedata);
startActivity(intent);

In ActivityTwo,

Intent intent = getIntent();
String data = intent.getStringExtra("data");
like image 7
Velayutham M Avatar answered Oct 18 '22 03:10

Velayutham M