- 新しいアクティビティを追加するには、この質問で回答されている方法に従ってください。このようにして、手動でマニフェストに追加せずに新しいアクティビティを作成します。[すべての活動は ] に記載する必要があります
AndroidManifest.xml。
新しいアクティビティ名を作成するとしますActivity2.java。新しいレイアウトを新しいアクティビティに追加するには、[新しいアクティビティのレイアウトを定義する場所] などのres/layoutフォルダーに新しい xml ファイルを追加します。activity2.xml
新しいレイアウトを新しいアクティビティにリンクするには、新しく作成したActivity2.java
setContentView(R.layout.activity2);
したがって、次のようになります。
public class Activity2 extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
}
}
2. Activity1.javaからにデータを送信する場合はActivity2.java、 を使用する必要がありますBundles。
Stringしたがって、 fromと言って送信したい場合Activity1は、 で次のようにしActivity1.javaます。
Intent nextActivity = new Intent(this, Activity2.class);
Bundle passData = new Bundle(); //to hold your data
passDataBndl.putString("fname", fname); //put in some String. the first parameter to it is the id, and the second parameter is the value
nextActivity.putExtras(passDataBndl); //Add bundle to the Intent
startActivityForResult(nextActivity, 0); //Start Intent
あなたの中でデータを受け取るにActivity2.javaは、次のことを行います(たとえば、onCreate())
Bundle params = this.getIntent().getExtras(); //gets the data from the Intent
String firstName = params.getString("fname"); //gets value of fname