답변:
int귀하의 아이디 인를 새로운에 넣으십시오 Intent.
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("key", 1); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);
finish();
그런 다음 새 ID를 가져옵니다 Activity.
Bundle b = getIntent().getExtras();
int value = -1; // or other values
if(b != null)
value = b.getInt("key");
활동을 호출하는 데 사용하려는 의도에 추가 데이터를 추가하기 만하면됩니다.
발신자 활동에서 :
Intent i = new Intent(this, TheNextActivity.class);
i.putExtra("id", id);
startActivity(i);
호출하는 활동의 onCreate () 안에서 :
Bundle b = getIntent().getExtras();
int id = b.getInt("id");
두 번째 활동에서 정적 메소드로 수행하고 싶습니다.
private static final String EXTRA_GAME_ID = "your.package.gameId";
public static void start(Context context, String gameId) {
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra(EXTRA_GAME_ID, gameId);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
Intent intent = this.getIntent();
String gameId = intent.getStringExtra(EXTRA_GAME_ID);
}
그런 다음 첫 번째 활동 (및 다른 곳)에서 다음을 수행하십시오.
SecondActivity.start(this, "the.game.id");
String gameId = intent.getStringExtra(EXTRA_EXTERNAL_ID);될String gameId = intent.getStringExtra(EXTRA_GAME_ID);
코 틀린 코드 :
시작 SecondActivity:
startActivity(Intent(context, SecondActivity::class.java)
.putExtra(SecondActivity.PARAM_GAME_ID, gameId))
에 ID를 얻으십시오 SecondActivity:
class CaptureActivity : AppCompatActivity() {
companion object {
const val PARAM_GAME_ID = "PARAM_GAME_ID"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val gameId = intent.getStringExtra(PARAM_GAME_ID)
// TODO use gameId
}
}
여기서 gameId하다 String? (널 (null) 일 수 있음)
기존 답변 (전달 된 데이터 Intent전달 startActivity()) 은이 문제를 해결하는 일반적인 방법을 보여줍니다. (예를 들어,의 편집 활동 중 하나 다른 응용 프로그램에 의해 시작됩니다 액티비티 만들 경우 홀수 경우에 사용할 수있는 다른 해결책이있다 테스 커의 제어하지 않는 때문에 플러그인을)하고 Intent있는 시작하는이 Activity.
Activity매개 변수가있는 생성자를 가진 기본 클래스를 만든 다음 기본 클래스 생성자를 호출하는 기본 생성자가있는 파생 클래스를 다음과 같이 만들 수 있습니다 .
class BaseActivity extends Activity
{
public BaseActivity(String param)
{
// Do something with param
}
}
class DerivedActivity extends BaseActivity
{
public DerivedActivity()
{
super("parameter");
}
}
기본 클래스 생성자에 전달할 매개 변수를 생성해야하는 경우, 하드 코딩 된 값을 전달할 올바른 값을 리턴하는 함수 호출로 바꾸십시오.