Programming$/Android

Intent로 Activity 간 데이터 교환

ch4rli3kop 2020. 4. 14. 04:04
반응형

Intent로 Activity 간 데이터 교환

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       Button btn2 = findViewById(R.id.button2);
       btn2.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Intent myintent = new Intent(MainActivity.this, Main2Activity.class);
               myintent.putExtra("QWER", "HI Hello oooooolll~~~!");
               startActivity(myintent);
          }
      });
  }

Main2Activity

public class Main2Activity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main2);

       Intent myIntent = getIntent();
       String str = myIntent.getExtras().getString("QWER");
       Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
  }
}


반응형

'Programming$ > Android' 카테고리의 다른 글

[Android] 외부 라이브러리 사용하기  (0) 2021.10.04
glide (feat. custom dialog)  (0) 2020.06.24
JNI function symbol 제거하기  (0) 2020.06.12
onClickListener implements로 구현하기  (0) 2020.04.14