[android] Pass a String from one Activity to another Activity in 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.

This question is related to android

The answer is


First Activity Code :

Intent mIntent = new Intent(ActivityA.this, ActivityB.class);
mIntent.putExtra("easyPuzzle", easyPuzzle);

Second Activity Code :

String easyPuzzle = getIntent().getStringExtra("easyPuzzle");

Most likely as others have said you want to attach it to your Intent with putExtra. But I want to throw out there that depending on what your use case is, it may be better to have one activity that switches between two fragments. The data is stored in the activity and never has to be passed.


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");

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");

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");

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.


Post Value from

Intent ii = new Intent(this, GameStartPage.class);

// ii.putExtra("pkgName", B2MAppsPKGName);

ii.putExtra("pkgName", YourValue);
ii.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ii);

Get Value from

pkgn = getIntent().getExtras().getString("pkgName");