- 新しいアクティビティを追加するには、この質問で回答されている方法に従ってください。このようにして、手動でマニフェストに追加せずに新しいアクティビティを作成します。[すべての活動は ] に記載する必要があります
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