在Android中将string从一个活动传递给另一个活动
这是我的string:
private final String easyPuzzle ="630208010200050089109060030"+ "008006050000187000060500900"+ "09007010681002000502003097";
我想在9 * 9数独板上显示另一个活动的string。
你需要把它作为一个额外的传递:
String easyPuzzle = "630208010200050089109060030"+ "008006050000187000060500900"+ "09007010681002000502003097"; Intent i = new Intent(this, ToClass.class); i.putExtra("epuzzle", easyPuzzle); startActivity(i);
然后从这样的新活动中提取它:
Intent intent = getIntent(); String easyPuzzle = intent.getExtras().getString("epuzzle");
在activity1中
String easyPuzzle = "630208010200050089109060030"+ "008006050000187000060500900"+ "09007010681002000502003097"; Intent i = new Intent (this, activity2.class); i.putExtra("puzzle", easyPuzzle); startActivity(i);
在activity2
Intent i = getIntent(); String easyPuzzle = i.getStringExtra("puzzle");
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);
现在转到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("abc"); }
现在“yourPreviousPzl”是你想要的string。
发布价值
Intent ii = new Intent(this, GameStartPage.class); // ii.putExtra("pkgName", B2MAppsPKGName); ii.putExtra("pkgName", YourValue); ii.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(ii);
从中获得价值
pkgn = getIntent().getExtras().getString("pkgName");
最有可能像其他人所说,你要把它附加到你的Intent
与putExtra
。 但是我想抛弃那些依赖于你的用例的东西,最好有一个在两个片段之间切换的活动。 数据存储在活动中,永远不必传递。