문제

내 Listactivity의 생성자에 변수를 전달하고 싶습니다.

이 코드를 통해 활동을 시작합니다.

startActivity(new Intent (this, viewContacts.class));

비슷한 코드를 사용하고 싶지만 두 줄을 생성자에게 전달합니다. 어떻게 가능합니까?

도움이 되었습니까?

해결책

매개 변수를 전달하려면 새로운 의도를 만들고 매개 변수 맵을 넣습니다.

Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("firstKeyName","FirstKeyValue");
myIntent.putExtra("secondKeyName","SecondKeyValue");
startActivity(myIntent);

시작 활동 내에서 매개 변수 값을 얻으려면 get[type]Extra() 같은 의도로 :

// getIntent() is a method from the started activity
Intent myIntent = getIntent(); // gets the previously created intent
String firstKeyName = myIntent.getStringExtra("firstKeyName"); // will return "FirstKeyValue"
String secondKeyName= myIntent.getStringExtra("secondKeyName"); // will return "SecondKeyValue"

매개 변수가 ints 인 경우 사용할 것입니다 getIntExtra() 대신 등. 이제 평소처럼 매개 변수를 사용할 수 있습니다.

다른 팁

나는 당신이 다음과 같은 것을 원한다고 생각합니다.

Intent foo = new Intent(this, viewContacts.class);
foo.putExtra("myFirstKey", "myFirstValue");
foo.putExtra("mySecondKey", "mySecondValue");
startActivity(foo);

또는 먼저 번들에 결합 할 수 있습니다. 해당 GetExtra () 루틴이 다른쪽에 존재합니다. 보다 의도 주제 자세한 내용은 개발자 가이드에서.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top